Let's assume these are ints  it could work for longs as well but then you'll use 64 bits instead of 32.
5 in bit notation is
1111 1111 1111 1111 1111 1111 1111 1011
Now please note that the shift will always be modulo 32 for ints and modulo 64 for longs. Therefore, 5 >> 2 is equal to 5 >> 30
So all bits are shifted 30 places to the right, filling the left with the leftmost bit of the original, 1:
1111 1111 1111 1111 1111 1111 1111 1111
Of this number, which is 1, only the last two bits are taken from the original.
Now if you want to add zeros at the start, use >>> instead of >>. This will result in:
0000 0000 0000 0000 0000 0000 0000 0011
which is 3.
SCJP 1.4  SCJP 6  SCWCD 5  OCEEJBD 6  OCEJPAD 6
How To Ask Questions How To Answer Questions
If the promoted type of the lefthand operand is int, only the five lowestorder bits of the righthand operand are used as the shift distance. It is as if the righthand 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 lefthand operand is long, then only the six lowestorder bits of the righthand operand are used as the shift distance. It is as if the righthand 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.
Now, who can tell me what on Earth the point of this is?
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I think they did this because the sizes of int and long are 32 and 64 bit respectively, and the people who invented Java didn't see any logic in shifting more than 31 / 63 (basically making your number either 0 or 1) or shifting less than 0 (shifting the other way around).
Of course the modulo is kinda weird; if I'd want to right shift an integer 33 I'd want 0 or 1, not half the number I had.
SCJP 1.4  SCJP 6  SCWCD 5  OCEEJBD 6  OCEJPAD 6
How To Ask Questions How To Answer Questions
Beware the other head of science  it bites! Nibble on this message:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
