Here is the implementation of the converse in Long:
public static long reverse(long i) { // HD, Figure 7-1 i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L;//1 i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L;//2 i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL;//3 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;//4 i = (i << 48) | ((i & 0xffff0000L) << 16) | ((i >>> 16) & 0xffff0000L) | (i >>> 48);//5 return i; }
I can understand the line 1,2,3,4, but not 5! How it works?
I group 64 bits into 8 groups, that is, 1 is the first 8 bits, 2 is two 8 bits, etc.
Then after line 4, a sequence similar to 4,3,2,1,8,7,6,5
and I think line 5 works like below before operation |
:
6,5,0,0,0,0,0,0-->(i << 48) 8,7,0,0,0,0,0,0-->((i & 0xffff0000L) << 16) 0,0,0,0,4,3,2,1-->((i >>> 16) & 0xffff0000L) 0,0,0,0,0,0,2,1-->(i >>> 48)
But, I do not know where the dose is wrong or wrong! Thinking about it almost the whole day!
Can anyone help me !! Thanks.
oh, I made a mistake as follows:
6,5,0,0,0,0,0,0-->(i << 48) 0,0,8,7,0,0,0,0-->((i & 0xffff0000L) << 16) 0,0,0,0,2,1,0,0-->((i >>> 16) & 0xffff0000L) 0,0,0,0,0,0,4,3-->(i >>> 48)
but I think this is wrong! I think the correct sequence is 8,7,6,5,4,3,2,1
I am very sorry that I am wrong! It works correctly as shown below:
after line 4, correct pattern: 2,1,4,3,6,5,8,7
8,7,0,0,0,0,0,0-->(i << 48) 0,0,6,5,0,0,0,0-->((i & 0xffff0000L) << 16) 0,0,0,0,4,3,0,0-->((i >>> 16) & 0xffff0000L) 0,0,0,0,0,0,2,1-->(i >>> 48)