• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Shift operators

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For any valid int value i, the following will always be true:

o/p:true.....how?
 
author
Posts: 23959
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
When shifting an integer, only the lowest 5 bits, of the amount to shift, is used. Hence...

-1 & 0x1f = 0x1f = 31

Henry
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
didn't get it henry....

pls explain in details if possible...

regards,
 
Henry Wong
author
Posts: 23959
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

Originally posted by amit taneja:
didn't get it henry....

pls explain in details if possible...

regards,



I don't know how much more detail I can give... but I'll try...



Since only the lowest 5 bits are used, shifting by -1 is the same as shifting by 31.

Henry
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

I am kind of understood but not completely. Why should we take only the lowest 5 bits?
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also didn't get fully...
what i get is that ..we cant shift -1 so we get its binary value and take 5 lower bits which is 31 and we can say that -1=31 with respect to shifting...

but why 31 is that is because we can shift maximum 31 digits so we take lower 5 bits which amounts to 31 but...didnt get
satisfied with the whole logic...

 
Henry Wong
author
Posts: 23959
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

Originally posted by Chitra AP:
I am kind of understood but not completely. Why should we take only the lowest 5 bits?



It is defined that way in the specification. When shifting ints, only the lowest 5 bits of the amount to shift is used. And when shifting longs, only the lowest 6 bits of the amount to shift is used.

Henry
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code:
a << b
Where a is a int,
shifts a by b%32 places left.

So -1%32 -> as -1 is negative, add 32 to it
so 31....reasoned it logically...

shifting an integer by ........-33 -1 31 63.......all these should
give same result logically

Similarily
given x, shifting x by
x+32,x+64,.....
x-32,x-64..... should give same result
 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An int can be shifted by 0-31 places
An long can be shifted by 0-63 places...

So 31 -> last 5 digits is used (11111 = 31)
and 63 -> last 6 digits is used (111111 = 64)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic