Java Integer reverseByte() method
The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter ‘i’ represents the bytes whose value is to be reversed. Return Value This method returns the value obtained by reversing order of the bytes in the specified int value. Example 1
public class JavaIntegerReverseBytesExample1 { public static void main(String[] args) { Integer i=120; // returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation int reverse= Integer.reverseBytes(i); System.out.println("2's complement binary representation for byte "+i+" : "+reverse); } }Output
2's complement binary representation for byte 120 : 2013265920Example 2
public class JavaIntegerReverseBytesExample2 { public static void main(String[] args) { Integer i=0; // returns 0 if i is equal to zero int reverse= Integer.reverseBytes(i); System.out.println("2's complement binary representation for byte "+i+" : "+reverse); } }Output
2's complement binary representation for byte 0 : 0Example 3
public class JavaIntegerReverseBytesExample3 { public static void main(String[] args) { Integer i=Integer.MAX_VALUE; // returns -129 if i is equal to MAX_VALUE int reverse= Integer.reverseBytes(i); System.out.println("2's complement binary representation for byte "+i+" : "+reverse); } }Output
2's complement binary representation for byte 2147483647 : -129