Hi
The same question was posted before. I kept the answer to it as a reference. It might help:
Using % to reduce the right-hand side (RHS) operand is fine for positive operands. However, when you have negative RHS operands, you should go by the low-order bits (5 for int and 6 for long). That is,
x >> -4; // reduced to x >> 28 when x is int
x >> -4; // reduced to x >> 60 when x is long
What Joshua Bloch (author of Effective Java and the Collections API) pointed out to me a while back was that reduction is done by masking the RHS operand with 0x1F for int shifts and 0x3F for long shifts, not by using %.
For int:
-4 = 1111 11.. 1111 1100 (32 bits)
& 0x1F = & 0000 00.. 0001 1111
-----------------------------------------
28 = 0000 00.. 0001 1100
For long:
-4 = 1111 11.. 1111 1100 (64 bits)
& 0x3F = & 0000 00.. 0011 1111
-----------------------------------------
60 = 0000 00.. 0011 1100
shifting operands must be a primitive integer type, which of course includes long and excludes float and double.
And the answer to your original post is 3 by the way.
I hope this helps.