Java.lang Package Classes

Java Long - reverseBytes() Method



The java.lang.Long.reverseBytes() method returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified long value.

Syntax

public static long reverseBytes(long i)

Parameters

i Specify the value whose bytes are to be reversed.

Return Value

Returns the value obtained by reversing the bytes in the specified long value.

Exception

NA.

Example:

In the example below, the java.lang.Long.reverseBytes() method returns the value obtained by reversing the bytes in the given long value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating a long value
    long x = 100; 

    //printing value of x, its binary representation
    //and reversing the bytes
    System.out.println("The values of x is: " + x);
    System.out.print("The binary representation of x is: ");
    System.out.println(Long.toBinaryString(x));
    System.out.print("After reversing : ");
    System.out.println(Long.reverseBytes(x));
  }
}

The output of the above code will be:

The values of x is: 100
The binary representation of x is: 1100100
After reversing : 7205759403792793600

❮ Java.lang - Long