• 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

Shift operator

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here for the following one we can use the formula x*2topowerof n
1<<1 yields 2
1<<2 yields 4
1<<3 yields 8
But for the following
how can findout the result please help me
any formula to calculate it
..
1<<67 yields 8
1<<99 yields 8
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karthik Rajashekaran:
... But for the following
how can findout the result please help me
any formula to calculate it
..
1<<67 yields 8
1<<99 yields 8


For int shifts, only the last 5 bits of the right operand are used, so the amount of the shift is always between 0 and 31. (For long shifts, the last 6 bits are used, so the shift is between 0 and 63.)

So you can use modulus...
67%32 is 3. So 1<<67 = 1<<3 = 8.
99%32 is also 3, so 1<<99 = 3.
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ho thanks mark

what about for long, float and byte ?
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take number % (size of the given data type)

so for integer primitives, you will do x%32
for long data types, you will do x%64.

that's it.

rule: In x%y, y must be of integral data type. could be either int or long.

if lesser than int than it is converted to int.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic