• 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

confusion about bit-wise operators

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i had this:

but if I changed the i = i >> 31 to i = i >> 30, or 29 or anything, it is still -1 and if I change the int i to -2 or -3 it is still -1, I thought i understood how the bit-wise operator worked but how come it is like that? I must be very confused about the operator. I always thought that int was stored something like...
10000000....1 for -1, and 10000...10 for -2, but if that is the case then the >> operator is very different from what I think it should be doing?
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mui,
formula to get binary representation for negative number is
-x = ~x + 1
after the formula, -1 become all ones
1111 1111 1111 1111 1111 1111 1111 1111
baased on this -1>>31, -1>>30, -1>>29 will give same result as "1" was inserted from the left side for right shift on negative number.

HTH
[ April 27, 2003: Message edited by: chi Lin ]
 
Wilson Mui
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, I guess my confusion was in getting the negative number to start with, thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic