Java Integer reverse() method
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
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
2's complement binary representation for Reverse 120 : 503316480Example 2
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
2's complement binary representation for Reverse 0 : 0Example 3
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
2's complement binary representation for Reverse 2147483647 : -2