I was wondering, what can the >>>=
operator be applied to?
It works for the following:
int >>>= int
long >>>= long
long >>>= int
long >>>= short
short >>>= long
short >>>= short
int >>>= byte
But not for anything including float
or double
.
Why is that, and how can I make it work for float
and double
?
Thank you!
It doesn't make much sense to shift bits around in a float or double considering what the bits represent.
Bit 63 (the bit that is selected by the mask 0x8000000000000000L) represents the sign of the floating-point number. Bits 62-52 (the bits that are selected by the mask 0x7ff0000000000000L) represent the exponent. Bits 51-0 (the bits that are selected by the mask 0x000fffffffffffffL) represent the significand (sometimes called the mantissa) of the floating-point number.
Should you do an shift right on a double, the signbit would be interpreted as exponent, and part of the exponent would spill over to the significand. The semantics of the shift operation would be meaningless.
If you really want to shift the bits around, you can however go via Double.doubleToLongBits
and Double.longBitsToDouble
as follows:
// Unsigned shift right 3 of a double
Double.longBitsToDouble(Double.doubleToLongBits(d) >>> 3)