Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# left shift operation

vijay gardiner
Greenhorn
Posts: 14
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

Jennifer Wallace
Ranch Hand
Posts: 102
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,i is 1
0000 0000 0000 0000 0000 0000 0000 0001 = 1
then i<<29 is
0010 0000 0000 0000 0000 0000 0000 0000
which is Big Positive number.
i<<31 moves i to the Most significant bit
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 ]

vijay gardiner
Greenhorn
Posts: 14
thanx!
that helps!
Vijay