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

Java Integer reverseBytes() Method

Java reverseBytes() 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 bytes of the two's complement binary representation of the integer value passed.

Syntax:

public static int reverseBytes(int i)  

Parameter:

The parameter passed is the integer value whose bytes are to be reversed.

Returns:

The value obtained by reversing the order of bytes 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. The 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 = 342;  
          int b = -23;  
          System.out.println("Original Number = " + a);             
          //converts number to equivalent binary string 
          System.out.println("Binary Representation is = " + Integer.toBinaryString(a)); 
          //reversal of the bytes
          System.out.println("Number after reversal " + Integer.reverseBytes(a)); 
          
          System.out.println("\n Original Number = " + b);               
          System.out.println("Binary Representation is = " + Integer.toBinaryString(b));           
          System.out.println("Number after reversal = " + Integer.reverseBytes(b));  
    }  
}  


Original Number = 342
Binary Representation is = 101010110
Number after reversal 1442906112

Original Number = -23
Binary Representation is = 11111111111111111111111111101001
Number after reversal = -369098753

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);  
           // returns the integer value into its binary equivalent 
           System.out.println("Binary Representation = " + Integer.toBinaryString(i)); 
           //returns the value obtained by reversal of bytes
           System.out.println("After reversing = " + Integer.reverseBytes(i)); 
        }
        catch(Exception e)
        {
           System.out.println("Invalid Input");
        }
        
    }  
}  


Enter Original Value 54
Actual Number = 54
Binary Representation = 110110
After reversing = 905969664
***************************************
Enter Original Value -43
Actual Number = -43
Binary Representation = 11111111111111111111111111010101
After reversing = -704643073
*************************************************
Enter Original Value 0x212
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.