Java.lang Package Classes

Java Integer - reverse() Method



The java.lang.Integer.reverse() method returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.

Syntax

public static int reverse(int i)

Parameters

i Specify the value to be reversed.

Return Value

Returns the value obtained by reversing order of the bits in the specified int value.

Exception

NA.

Example:

In the example below, the java.lang.Integer.reverse() method returns the value obtained by reversing order of the bits in the given int value.

import java.lang.*;

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

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

The output of the above code will be:

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

❮ Java.lang - Integer