Converting Little Endian to Big Endian

后端 未结 6 1414
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 19:32

All,

I have been practicing coding problems online. Currently I am working on a problem statement Problems where we need to convert Big Endian <-> little endian.

6条回答
  •  一个人的身影
    2020-11-27 19:39

    the following method reverses the order of bits in a byte value:

    public static byte reverseBitOrder(byte b) {
        int converted = 0x00;
        converted ^= (b & 0b1000_0000) >> 7;
        converted ^= (b & 0b0100_0000) >> 5;
        converted ^= (b & 0b0010_0000) >> 3;
        converted ^= (b & 0b0001_0000) >> 1;
        converted ^= (b & 0b0000_1000) << 1;
        converted ^= (b & 0b0000_0100) << 3;
        converted ^= (b & 0b0000_0010) << 5;
        converted ^= (b & 0b0000_0001) << 7;
    
        return (byte) (converted & 0xFF);
    }
    

提交回复
热议问题