• 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

Confusing shift operators.

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What will be the result of
1<<32....and why is it equal to 1>>32 or 1>>>32.
As per my understanding, the bits will be shifted left and after 31 shifts the number will be reduced to zero as then it will be filled with zeros from right. Am i right?
In what conditions can unsigned right shift operator produce negative values?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 << 32 The left shift (<< shifts all bits to the left, filling the right side with zeroes,
1>>32 right shift (>> shifts all bits right, filling in the left side with whatever the sign bit was.
OR
1>>>32 The unsigned right shift (>>> moves all bits to the right, but fills the left side with zeroes,
Regards
PREM
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think shifting the bits by 32 places, either left or right will result in the same number. Since, shift by number greater than or equal to 32 will always be a modulo of 32. That is, 1>>32 will be 1>>0 (1 >> (32%32)) , resulting in 1 itself.
Is my assumption right??
Thanks,
Uma...
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Uma,
you are right but you didnt give him the complete picture.
Hi Harjinder,
in case of integers, it works like this:
opr1>>opr2 where opr2 = (opr2%32) if opr2 >= 32. for both left shift and right shift.
in case of long, it works like this:
opr1>>opr2 where opr2 = (opr2%64) if opr2 >= 64. for both left shift andirght shift.
now just think about short and try out how it works for a short?
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arun Subbu:
now just think about short and try out how it works for a short?
[/QB]


byte, short, char is the same as int i.e.
opr2=opr2%32
A unary numeric promotion to int is performed for these primitive types when used in a shift operation.
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic