The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation.
Syntax
public static int reverse (int i)
Parameters
The parameter ‘i’ represents the value to be reversed.
Return Value
This method returns the value obtained by reversing order of the bits in the specified int value.
Example 1
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerReverseExample1 { public static void main(String[] args) { Integer i=120; // returns the value obtained by reversing order of the bits in the specified int value int reverse= Integer.reverse(i); System.out.println("2's complement binary representation for Reverse "+i+" : "+reverse); } } |
Output
1 2 3 |
2's complement binary representation for Reverse 120 : 503316480 |
Example 2
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerReverseExample2 { public static void main(String[] args) { Integer i=0; // returns 0 if i is equal to zero int reverse= Integer.reverse(i); System.out.println("2's complement binary representation for Reverse "+i+" : "+reverse); } } |
Output
1 2 3 |
2's complement binary representation for Reverse 0 : 0 |
Example 3
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerReverseExample3 { public static void main(String[] args) { Integer i=Integer.MAX_VALUE; // returns -2 if i is equal to MAX_VALUE int reverse= Integer.reverse(i); System.out.println("2's complement binary representation for Reverse "+i+" : "+reverse); } } |
Output
1 2 3 |
2's complement binary representation for Reverse 2147483647 : -2 |