aspose file tools*
The moose likes Beginning Java and the fly likes left shift operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "left shift operation" Watch "left shift operation" New topic
Author

left shift operation

vijay gardiner
Greenhorn

Joined: Jan 26, 2002
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

Joined: Nov 30, 2001
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

Joined: Jan 26, 2002
Posts: 14
thanx!
that helps!
Vijay
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: left shift operation
 
Similar Threads
shift operator
Question on Shift Operators
Ques. from JTips Mock 1
shift opeartors - need help
Need some basic algebra methods....