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

# Bit shifting?

Ranch Hand
Posts: 252
• Number of slices to send:
Optional 'thank-you' note:

here how can the answer be 4...
my doubt is here the long variable after moving ..how can it be assigned it to int ...without explicit casting ....

thanks
sri

author
Posts: 23956
142
• Number of slices to send:
Optional 'thank-you' note:
The long is *not* being assigned to anything. It is merely being used to to state how many bits to shift. And even in this regard, only the lower 5 bits of the long will be used.

No assignment of the long. Hence, no casting necessary.

Henry

Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Henry Wong:
And even in this regard, only the lower 5 bits of the long will be used.

What's meant with that sentence?

because i tought the number of bits that will be shifted is 3 (99 % 32 (= max bits of an int)), so have no idea what's meant with lower 5 bits of long will be used

Ranch Hand
Posts: 657
• Number of slices to send:
Optional 'thank-you' note:

because i tought the number of bits that will be shifted is 3 (99 % 32 (= max bits of an int)), so have no idea what's meant with lower 5 bits of long will be used

99 & 31 = 3

99 in binary:
00000000 00000000 00000000 01100011

31 in binary:
00000000 00000000 00000000 00011111

When they are ANDed together, the result is (in binary):
00000000 00000000 00000000 00000011

Or, 3. This is how everything but the five lowest bits are masked off. It's mathematically equivalent to %32, but the JLS specifically describes the behavior as using the five lowest bits for int values (six lowest for long).

Hope this helps...

author
Posts: 9050
21
• Number of slices to send:
Optional 'thank-you' note:
And for those of you studying for the Tiger exam, remember this topic doesn't apply to you, it's not on the Tiger exam!

Roel De Nijs
Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Steve Morrow:
Hope this helps...

it did help.
thanks!

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.