This content has been marked as final. Show 4 replies
int java.lang.Integer.reverse(int i)
public static int reverse(int i)
Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.
the value obtained by reversing order of the bits in the specified int value.
** that's from the documentation
from what I understand, for example, I have
the binary representation of 1 in two's complement is
int number = 1;
0000 0000 0000 0000 0000 0000 0000 0001
(since int is 32 bits)
if we do
what I got as result is -2147483648. Probably what it did was to reverse the order of the bits (not the value), hence the binary value became
1000 0000 0000 0000 0000 0000 0000 0000 (this is reversing the order of the bits)
1111 1111 1111 1111 1111 1111 1111 1110 (this is reversing the value of the bits, and this is NOT what Integer.reverse() does)
correct me if I'm wrong guys..
Hello,This is correct. first our Integer value converted into binary value. then reverse that binary & calculates the decimal value.
Check the API -
From the explanation of the API, 2
(00000000000000000000000000000010 in binary) will
become 1073741824 (01000000000000000000000000000000
in binary) unless I don't understand the API correct.
it will gives the int value.
if our Integer value is odd number, the return unt value is -ve.
if our Integer value is even number, the return unt value is +ve.