• 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

right bit shift operator

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1>>31 is 0
1>>32 is 1
1>>33 is 0
what is the logic behind these results?
Thanks for the help.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As "1" is Type int, it is assign 32 bit as default.
So, if the shift bit is greater than or equal to 32, it will be "mod"ed.
As a result, if it is 33, then it become 33 mod 32 = 1 (shift 1 bit),
shift 34 means 34 mod 32 = 2 (shift 2 bit as result).
As show below,
for case 1, shift 31, means to add 31 signed bit (0) in the left.
for case 2, 32 mod 32 = 0, it finally shift 0, that means unchanged.
for case 3, 33 mod 32 = 1, it means shift 1 right, so it gets to 0.
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey P intelli,
what hkvince said is true.
when using the >> operators your operand will be an int or promoted to int.
so 1 >> 31 is like:
00000000 00000000 00000000 00000001 it is a signed bitshift so move 31 0's to get
00000000 00000000 00000000 00000000 = 0
1 >> 32 use the above rule 32 % 32 = 0
so you get:
00000000 00000000 00000000 00000001 move it >> 0 to get same result
00000000 00000000 00000000 00000001 = 1
1 >> 33 again use the 32 %33 = 1
00000000 00000000 00000000 00000001 so bit shift it by >> 1 to get
00000000 00000000 00000000 00000000 = 0
remember your operator was the signed right shift so you have to move what the signed bit was, in this case a 0.
Davy
 
Yan Lee
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and thanks for answering my question.
After reading your responses, these is what that I have understood:
1. for right hand operand greater than 32 is equivalent to shifting with
right hand operand%32
2. for right hand operand equal to 32, the number remains the same
3. for anything less than 32, the bits are shifted right by that number and
sign bits are added to the left.
Thanks once again.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, refer to my explanation about shifting by a negative value. The same explanation should clear up all of your questions.
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic