Reverse Byte Length Order

I have a variable longand I need to reverse the byte order. For example: B1, B2, ... , B8I have to return a long one consisting of B8, B7, ..., B1. How to do this using bitwise operations?

+3
source share
5 answers

you can use Long.reverseBytes (long)

Or for other methods that involve bitwise operations, you can refer to this stack overflow question

Here is another way that you might like, I would recommend above, but this is better than bitwise, where you can easily make mistakes.

Bytebuffer

byte[] bytes = ByteBuffer.allocate(8).putLong(someLong).array();
for (int left = 0, right = bytes.length - 1; left < right; ++left, --right) {
    byte temp = bytes[left]; 
    bytes[left]  = bytes[right]; 
    bytes[right] = temp;
}

, , , ... :

byte[] bytes = new byte[8];

// set the byte array from smallest to largest byte
for(int i = 0; i < 8; ++i) {
    byte[i] = (your_long >> i*8) & 0xFF;
}

// build the new long from largest to smallest byte (reversed)
long l = ((buf[0] & 0xFFL) << 56) |
         ((buf[1] & 0xFFL) << 48) |
         ((buf[2] & 0xFFL) << 40) |
         ((buf[3] & 0xFFL) << 32) |
         ((buf[4] & 0xFFL) << 24) |
         ((buf[5] & 0xFFL) << 16) |
         ((buf[6] & 0xFFL) <<  8) |
         ((buf[7] & 0xFFL) <<  0) ;
+6

Long.reverseBytes. . Java.

JDK (src.zip JDK) Long.java, Oracle.

+2

, , endian :

static long swapblock(long a, long mask, int shift) {
    long b1 = a & mask; // extract block
    long b2 = a ^ b1;   // extract remaining bits
    return (b1 << shift) |
           ((b2 >> shift) & mask); // mask again to clear sign extension
}

static long endianswap(long a) {
    a = swapblock(a, 0x00000000ffffffffL, 32);
    a = swapblock(a, 0x0000ffff0000ffffL, 16);
    a = swapblock(a, 0x00ff00ff00ff00ffL, 8);
    return a;
}

, , . 4, 2 1, .

- java. , , (0x8000000000000000 >> 8 is 0xFF80000000000000).

+1
source
long reverse(long x){
     x = (x >> 32) | (x << 32); // step 1
     x = ((x & 0xffff0000ffff0000 ) >> 16) 
          | ((x & 0x0000ffff0000ffff ) << 16); // step 2
     x = ((x & 0xff00ff00ff00ff00 ) >> 8) 
          | ((x & 0x00ff00ff00ff00ff ) << 8); // step 3
     return x;
}

Assuming that the bitwise operator works O (1) times, the inverse function works O (lg (number of bits)). Explanation
Step 0: B1 B2 B3 B4 B5 B6 B7 B8
Step 1: B5 B6 B7 B8 B1 B2 B3 B4
Step 2: B7 B8 B5 B6 B3 B4 B1 B2
Step 3: B8 B7 B6 B5 B4 B3 B2 B1

0
source

The usual answer with loops:

public static long byteReverse(long a) {

    long result = 0;
    for(int i = 0; i < 8; i++){
        // grab the byte in the ith place
        long x = (a >> (i*8)) & (0b11111111);
        result <<= 8;
        result |= x;
    }
    return result;
}

bitwise only:

public static long byteReverse(long a) {

    a = (a << 32) | (a >>> 32);
    a = ((a & 0xffff0000ffff0000L) >>> 16) | ((a & 0x0000ffff0000ffffL) << 16);
    a = ((a & 0x00ff00ff00ff00ffL) << 8) | ((a & 0xff00ff00ff00ff00L) >>> 8);

    return a;
}
0
source

All Articles