Open In App

Integer reverse() Method In Java

Last Updated : 07 Jul, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The java.lang.Integer.reverse() is an inbuilt method in Java and is used to return the reverse order of the bits in the two's complement binary representation of the specified int value. Syntax:
public static int reverse(int a)
Parameters: The parameter a is an integer value whose bits are to be reversed. Return Value: The method returns the value which is obtained by reversing the order of the bits in the specified int value. Examples:
Input: 86
Output: 1778384896
Explanation:
Consider an integer a = 86
Binary Representation = 1010110
The number of one bit = 4 
After reversing it is = 1778384896

Input: 168
Output: 352321536
Below programs illustrate the java.lang.Integer.reverse() method: Program 1: For a positive number.
Output:
Number = 168
By reversing we get = 352321536
Program 2: For a Negative number.
Output:
Number = -72
By reversing we get = 503316479
Program 3: For a decimal value and string. Note: It throws an error message when a decimal value or string is passed as an argument.
Output:
prog.java:9: error: incompatible types: possible lossy conversion from double to int
    int a = 37.9;
            ^
prog.java:17: error: incompatible types: String cannot be converted to int
     a = "21";
         ^
2 errors

Next Article
Practice Tags :

Similar Reads