Integer reverseBytes() Method in Java

The java.lang.Integer.reverseBytes(int a) is a built-in method which returns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified int value.

Syntax :

public static int reverseBytes(int a)

Parameter: The method takes one parameter a of integer type whose bytes are to be reversed.

Return Value: The method will return the value obtained by reversing the bytes in the specified int value.

Examples:

Input: 75
Output: 1258291200
Explanation:
Consider an integer a = 75 
Binary Representation = 1001011
Number of one bit = 4 
After reversing the bytes we get = 1258291200

Input: -43
Output: -704643073

Below programs illustrate the java.lang.Integer.reverseBytes() method:
Program 1: For a positive number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// Java.lang.Integer.reverseBytes() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        int a = 61;
        System.out.println(" Integral Number = " + a);
  
        // It will return the value obtained by reversing the bytes in the
        // specified int value
        System.out.println("After reversing the bytes we get = "
        Integer.reverseBytes(a));
    }
}

chevron_right


Output:

Integral Number = 61
After reversing the bytes we get = 1023410176

Program 2: For a negative number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// Java.lang.Integer.reverseBytes() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        int a = -43;
        System.out.println(" Integral Number = " + a);
  
        // It will return the value obtained by reversing the bytes in the
        // specified int value
        System.out.println("After reversing the bytes we get = "
        Integer.reverseBytes(a));
    }
}

chevron_right


Output:

Integral Number = -43
After reversing the bytes we get = -704643073

Program 3: For a decimal value and string.
Note: It returns an error message when a decimal value and string is passed as an argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// Java.lang.Integer.reverseBytes() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        int a = 37.81;
        System.out.println(" Integral Number = " + a);
        // It will return the value obtained by reversing the bytes in the
        // specified int value
        System.out.println("After reversing the bytes we get = "
        Integer.reverseBytes(a));
  
        a = "81"; // compile time error will be generated
        System.out.println(" Integral Number = " + a);
        System.out.println("After reversing the bytes we get = "
        Integer.reverseBytes(a));
    }
}

chevron_right


Output:

prog.java:9: error: incompatible types: possible lossy conversion from double to int
    int a = 37.81;
            ^
prog.java:18: error: incompatible types: String cannot be converted to int
    a = "81";
        ^
2 errors



My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.