why 3 right shift 32 is equal to 3 and not 0.
I got these results in nodeJs and Java
- 3 >> 31 = 0
- 3 >> 32 = 3
- 3 >> 33 = 1
- 3 >> 34 = 0
- 3 >> 35 = 0
why 3 right shift 32 is equal to 3 and not 0.
I got these results in nodeJs and Java
This is part of the Java language specification. The right hand operand is wrapped so that it is always in the range 0 - bits, where bits is the number of bits of the left hand operand. Since you are shifting a 32 bit integer, the right hand operand is wrapped within 0 to 31. 32 becomes 0, 33 becomes 1 etc.
See the Java language specification for shift operators:
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 (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.