New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Java Integer reverse() Method

Java reverse() method is a part of the Integer class of the java.lang package. This method is used to return the value which is obtained by reversing the order of the bits of the two's complement binary representation of the integer value passed.

Syntax:

public static int reverse(int i)  

Parameter:

The parameter passed is the integer value whose binary equivalent is to be reversed.

Returns:

The value obtained by reversing the order of bits of the two's complement binary representation of the parameter passed.

Example 1:

Here, a positive and a negative number is taken for a better understanding of the method. Integer.toBinaryString() method is used for representing the number in its binary equivalent.

import java.lang.Integer;

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
          int a = 202;  
          int b = -50;  
          System.out.println("Original Number = " + a);             
          System.out.println("Binary Representation is = " + Integer.toBinaryString(a)); 
          System.out.println("Number after reversal " + Integer.reverse(a));//reversal of the number
          
          System.out.println("\n Original Number = " + b);               
          System.out.println("Binary Representation is = " + Integer.toBinaryString(b));           
          System.out.println("Number after reversal = " + Integer.reverse(b));  
    }  
}  


Original Number = 202
Binary Representation is = 11001010
Number after reversal 1392508928

Original Number = -50
Binary Representation is = 11111111111111111111111111001110
Number after reversal = 1946157055

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner;  

public class StudyTonight 
{  
    public static void main(String[] args) 
    {  
        try
        {
           System.out.print("Enter Original Value ");  
           Scanner sc = new Scanner(System.in);  
           int i = sc.nextInt();  
           System.out.println("Actual Number = " + i);  
           System.out.println("Binary Representation = " + Integer.toBinaryString(i)); // returns the integer value into its binary equivalent 
           System.out.println("After reversing = " + Integer.reverse(i)); //returns the value obtained by reversal of bits
        }
        catch(Exception e)
        {
           System.out.println("Invalid Input");
        }
        
    }  
}  


Enter Original Value 78
Actual Number = 78
Binary Representation = 1001110
After reversing = 1912602624
*****************************************
Enter Original Value -98
Actual Number = -98
Binary Representation = 11111111111111111111111110011110
After reversing = 2046820351
****************************************************
Enter Original Value 0x456
Invalid Input

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.