vijay gardiner

Greenhorn

Posts: 14

posted 15 years ago

x=1;

x << 29 yields 536870912, whose binary representation is all zeroes except '1' in the 30th higher order bit.

x << 30 yields 1073741824, whose binary representation is all zeroes except '1' in the 31th higher order bit.

What happens when x << 31 ?? Can anyone explain please in detail with the regd to the bits in binary form ??

Thanx,

Vijay

x << 29 yields 536870912, whose binary representation is all zeroes except '1' in the 30th higher order bit.

x << 30 yields 1073741824, whose binary representation is all zeroes except '1' in the 31th higher order bit.

What happens when x << 31 ?? Can anyone explain please in detail with the regd to the bits in binary form ??

Thanx,

Vijay

Jennifer Wallace

Ranch Hand

Posts: 102

posted 15 years ago

Integers in Java are signed. So the Most Significant Bit is the sign bit.

Here, I am just showing the Binary representation for the integers considered. U cud also get it with the static function of Integer class.... Integer.toBinaryString(int i);

For example,

0000 0000 0000 0000 0000 0000 0000 0010 = 2

1111 1111 1111 1111 1111 1111 1111 1110 = -2

-2 is here represented in two's complement form.

To get to the exact representation of any number in 2's complement notation u cud do it this way,

0000 0000 0000 0000 0000 0000 0000 0010 = 2

Invert it,

1111 1111 1111 1111 1111 1111 1111 1101

Then add 1 to it, +1

1111 1111 1111 1111 1111 1111 1111 1110 = -2

Here,in ur example,initially,

0000 0000 0000 0000 0000 0000 0000 0001 = 1

then

0010 0000 0000 0000 0000 0000 0000 0000

which is Big Positive number.

1000 0000 0000 0000 0000 0000 0000 0000

which is the representation for -2147483648

Hope this helps!

[ February 18, 2002: Message edited by: Jennifer Wallace ]

Here, I am just showing the Binary representation for the integers considered. U cud also get it with the static function of Integer class.... Integer.toBinaryString(int i);

For example,

0000 0000 0000 0000 0000 0000 0000 0010 = 2

1111 1111 1111 1111 1111 1111 1111 1110 = -2

-2 is here represented in two's complement form.

To get to the exact representation of any number in 2's complement notation u cud do it this way,

0000 0000 0000 0000 0000 0000 0000 0010 = 2

Invert it,

1111 1111 1111 1111 1111 1111 1111 1101

Then add 1 to it, +1

1111 1111 1111 1111 1111 1111 1111 1110 = -2

Here,in ur example,initially,

**i is 1**0000 0000 0000 0000 0000 0000 0000 0001 = 1

then

**i<<29**is0010 0000 0000 0000 0000 0000 0000 0000

which is Big Positive number.

**i<<31**moves i to the Most significant bit1000 0000 0000 0000 0000 0000 0000 0000

which is the representation for -2147483648

Hope this helps!

[ February 18, 2002: Message edited by: Jennifer Wallace ]