File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Java Interview Guide this week in the Jobs Discussion forum!

# hexa

saied ims
Ranch Hand

Joined: Jun 21, 2005
Posts: 109
class BitShift {
public static void main(String [] args) {
int x = 0x80000000;
System.out.println("Before shift x equals " + x);
x = x << 1;
System.out.println("After shift x equals " + x);
}
}
%java BitShift
Before shift x equals -2147483648
After shift x equals 0

now 0x80000000 how it is equal to -2147483648
1000 0000 0000 0000 0000 0000 0000 0000 <-this 1 one is the sign, isnt int is 32 bit so -2^31 to 2^31�1.....so 1 is value to 2power 32 how come 32 isnt 31......32 with sign so in this exampe the 1 is sign no value
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

> Before shift x equals -2147483648

In binary, it looks like this:
10000000 00000000 00000000 00000000

It's in two's complement format, so the leftmost bit is the sign. Remember that it indicates a negative (1-neg, 0-pos). Then, flip the bits and add one.

flip:
01111111 11111111 11111111 11111111

10000000 00000000 00000000 00000000

Read this like a regular binary number (2147483648), but remember that it was negative. That's why 0x80000000 is -2147483648 in decimal.

> After shift x equals 0

When you shift the bits left, it fills with zeros. High bits drop off the left side, never to return. So, when you shift this left by one...
10000000 00000000 00000000 00000000

...you get...
00000000 00000000 00000000 00000000

The high order bit dives into the bit bucket, and we're left with zero.

Hope this helps.

It is sorta covered in the JavaRanch Style Guide.

subject: hexa