wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hexa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hexa " Watch "hexa " New topic
Author

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

add one:
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hexa
 
Similar Threads
shift operators
Shift operators !
Question on Shift Operators
Different between signs(Positive and Negative ) and values
Shift Operators