Originally posted by Eric Low:
My 2 cents:
1 << -2 is equiv to 1 << (32 - 2). Dunno why? Could someone explain?
Eric
From the
JLS section 15.19
... 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.
So in your example a left shift of -2:
-2 & 0x1F is
11111111 11111111 11111111 11111110
00000000 00000000 00000000 00011111
00000000 00000000 00000000 00011110
is 30
In most cases this is the same (as Jay said) as the right hand side % 32.
hope that helps