• 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

shifting an -1 by 31

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody please explain


[ November 30, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget all that mod stuff.

34 in binary is 100010. The last 5 bits are 00010, that is 2.
So shift 8 (1000) by two bits to the right. That leaves 10, which is 2.
So 8>>34 is 2.


31 in binary is 11111. The last 5 bits are 11111, that is 31.
So shift -1(11...1111) by 31 bits to the right (remember there is no zero coming in on the left, the sign bit stays at 1). That results in 11...1111, which is -1 again. So -1>>31 is -1.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bary,
I did not understand your logic. why r u taking only 5 bits? why not more?
Please explain me the logic, and the method to follow.
regards,
Sri.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why r u taking only 5 bits?



Barry is taking 5 bits because the java specification says so... 5 bits for int, 6 bits for long.

And the reason it is 5 bits is because that is all you need for an int. There are only 32 bits, so the max you can shift and still have something left is 31.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic