Long reverse And reverseBytes Reorder Bits

Reverse all 64 bits or just the eight bytes of a long with Long.reverse and Long.reverseBytes for bit-order and endianness conversions without loops.

JavaData Fetching

Code

snippet
public class BitOrderFlip {

    public static void main(String[] args) {
        // reverse:64 位位序整体倒置——最高位与最低位互换
        long flags = 0b1011_0110;
        System.out.println("reverse(bits):  " + Long.toBinaryString(Long.reverse(flags)));

        // reverseBytes:字节序倒置,字节内部的位序保持不变
        long payload = 0x0123_4567_89AB_CDEFL;
        System.out.println("reverse(bytes): " + Long.toBinaryString(Long.reverseBytes(payload)));

        // 反转字节序后取低 8 位:拿到的是原值的最高字节(大端 ↔ 小端转换)
        System.out.println("old head byte:  " + (Long.reverseBytes(payload) & 0xFFL));

        // 纯位运算没有异常路径:全 1 倒置仍是全 1,0 倒置仍是 0
        System.out.println("reverse(-1):    " + Long.toBinaryString(Long.reverse(-1L)));
        System.out.println("reverse(0):     " + Long.reverse(0L));
    }
}

How it works

The working surface is the class BitOrderFlip and the function main — everything else is wiring.

Caveats

  • All behavior comes from the standard library; nothing is external.

Putting it to use

Entry signature: public static void main(String[] args)

Output lines are prefixed (reverse(bits):, reverse(bytes):, old head byte:), making what each step changes easy to notice.

Core lines

long payload = 0x0123_4567_89AB_CDEFL;
long flags = 0b1011_0110;

Interactive Tools for This Topic

FAQ

What does Long reverse And reverseBytes Reorder Bits print when run?
Invoke the file as-is — the `reverse(bits):`, `reverse(bytes):`, `old head byte:` prefixes identify which step printed each line.