An Arithmetic right shift will preserve the sign when shifting a signed number:
11111111 (-1) will stay 11111111 (-1)
In contrast, a Logical right shift won't preserve the sign:
11111111 (-1) will become 01111111 (127)
Your code clearly does an arithmetic shift, so the sign bit (MSB) is repeated. What the operator (>>) does depends on the implementation details of the platform you're using. In most cases, it's an arithmetic shift.
Also, note that 11111111
can have two different meanings depending on the representation. This also affects they way they'll be shifted.
- If unsigned,
11111111
represents 255. Shifting it to the right won't preserve the sign since the MSB is not a sign bit.
- If signed,
11111111
represents -1. Arithmetically shifting it to the right will preserve the sign.