The bit shift operators, e.g., >>
, are compatible with any integral types. However, for smaller types like byte
and short
, the operation performs a widening conversion: the result is elevated to type int
, and thus cannot be reassigned back to the source type.
Therefore, the following is not legal:
byte b = 3;
b = b >> 1;
But these assignments are legal:
byte b = 3;
b = (byte)(b >> 1)
int c = b >> 1;
As to why these operators don't work with float
or double
, I will leave that for you to explore. Hint: consider the layout of those types in memory and what the implications would be for bit shifting operators.