• 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

More confusion over bit shifting in RHE

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In RHE on p50 the following statements are made:
...shifts of ints use only the low order 5 bits, and shifts of longs use only low order 6 bits, of the right operand.
Can anyone explain this? Any ideas to its significance?
Regards
Paul
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ints has 32 bits..ok
so shifting more than 32 times on a number (LHS) doesn't make any sense. So the complier takes the modulo 32 of Right Hand operand (which will be 0 to 31) and uses it for shifting.
Same applies for the long with only differance of number of bits for long which are 64.
Hope it helps
 
Douggie Fox
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood the bit about the modulo, the things I didn't understand was "low order 5 bits" and "low order 6 bits". I do not know what these terms mean. Can anyone illustrate?
Regards
Paul
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a bit pattern, the "order" of bits are their relative positions within the pattern, with position 0 being the rightmost bit (lowest order bit). Positions also correspond to the power of 2 associated with that position.
The leftmost bit has the highest order. For int (32 bits), the highest order bit would be in position 31 (since positions start from 0). Since shift operations use modulo reduction of the right hand operand, for ints it is modulo 32 (2^5, hence only 5 low order bits are used) and for long it is modulo 64 (2^6, hence only 6 low order bits are used).
There! Made it to 500 posts
Edited: got my left and right mixed up in my excitement to post 500
To clarify: bit positions (order) increase from right to left starting from 0 as the rightmost bit and the leftmost having the highest order. Leftmost bit for signed numbers is the "sign bit" (0 for positive, 1 for negative)
[This message has been edited by JUNILU LACAR (edited June 22, 2001).]
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic