>> is the signed right shift operator. It shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand.
When you shift right two bits, you drop the two least significant bits.
Let’s say, x = 00111011
So when you do, x >> 2, it results in x = 00001110
This is essentially the same thing as dividing a value by 4 or by 2 two times while dropping the fractional part.
So, below code will result in 4:
byte val = 100;
val = (byte) (val >> 2);
System.out.println(val);
Explaining your example:
- The binary representation of 12 is: 1100
- 12 >> 1 is equivalent to 0110 which is 6 in decimal
- so (12 >> 1) – 1) is equivalent to 6-1 that is 5