Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Rules Round-up #114 (bit-shifting)

henrik lundahl
Greenhorn
Posts: 2
Question #114 in the Rules Round-up game is erroneous. When bit-shifting to the right by more bits than are available you'll get 0 (zero). Thus, the answer to the question should be 0.
The current answer says that the number of bits shifted will become (requested bits) modulo (available bits).

Nain Hwu
Ranch Hand
Posts: 139
Henrik,
The official answer is correct. See the spec at JLS 15.19:

If the promoted type of the left-hand operand is int, only the five lowest-order bits of the
right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected
to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f. The shift distance
actually used is therefore always in the range 0 to 31, inclusive.
If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the
right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected
to a bitwise logical AND operator & (�15.22.1) with the mask value 0x3f. The shift distance
actually used is therefore always in the range 0 to 63, inclusive.

henrik lundahl
Greenhorn
Posts: 2
Yes, you're correct, I give up.
When I tested I remembered the question wrongly and used a byte instead of an int. That resulted in the byte being promoted to an int and therefore could be shifted nine steps to the right to a zero.
The explanation to this question could be more exhaustive, I think, though. It should be stated that bytes, chars and shorts get promoted to ints and therefore will be shifted
<requested steps> % 32.
I hope I'll get this kind of question at the SCJP2 exam I have on Thursday, cause now I think I can answer it correctly.
Another thing; in the case you get 36 out of 36 on the game and starts a new round, a bug stops you from doing so, because the "DONE"-button isn't visible.