• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hexa

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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.
reply
    Bookmark Topic Watch Topic
  • New Topic