About Integer.reverse()

Hi How Integer.reverse() method working expecting detail explanation regards
[104 byte] By [EXCEPTIONa] at [2007-11-26 18:24:38]
# 1

int java.lang.Integer.reverse(int i)

reverse

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.

Returns:

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

int number = 1;

the binary representation of 1 in two's complement is

0000 0000 0000 0000 0000 0000 0000 0001

(since int is 32 bits)

if we do

System.out.println(Integer.reverse(number));

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..

albertsesea at 2007-7-9 5:58:42 > top of Java-index,Java Essentials,New To Java...
# 2
Hello,Check the API - http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.htmlFrom the explanation of the API, 2 (00000000000000000000000000000010 in binary) will become 1073741824 (01000000000000000000000000000000 in binary) unless I don't understand the API correct.
virusakosa at 2007-7-9 5:58:42 > top of Java-index,Java Essentials,New To Java...
# 3

> Hello,

>

> Check the API -

> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Inte

> ger.html

>

> From the explanation of the API, 2

> (00000000000000000000000000000010 in binary) will

> become 1073741824 (01000000000000000000000000000000

> in binary) unless I don't understand the API correct.

This is correct. first our Integer value converted into binary value. then reverse that binary & calculates the decimal value.

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.

ramesh.shrirama at 2007-7-9 5:58:42 > top of Java-index,Java Essentials,New To Java...
# 4
> This is correct.> first our Integer value converted into binary value.All values are binary so there is no conversion.Kaj
kajbja at 2007-7-9 5:58:42 > top of Java-index,Java Essentials,New To Java...