The right shift operator >> causes the bits of the left operand to be shifted right by the number of positions specified by the right operand.
variable >> number_of_bits;
- variable: Allowed data types: byte, int, long.
- number_of_bits: a number that is < = 32. Allowed data types: int.
※ NOTES AND WARNINGS:
When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have discussed above. In that case, the sign bit is copied into lower bits, for esoteric historical reasons:
This behavior, called sign extension, is often not the behavior you want. Instead, you may wish zeros to be shifted in from the left. It turns out that the right shift rules are different for unsigned int expressions, so you can use a typecast to suppress ones being copied from the left:
If you are careful to avoid sign extension, you can use the right-shift operator >> as a way to divide by powers of 2. For example: