简体   繁体   English

Java >>> =运算符

[英]Java >>>= operator

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 . 但不包括floatdouble任何东西。

Why is that, and how can I make it work for float and double ? 为什么会这样,我怎样才能让它适用于floatdouble

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. 位63(由掩码0x8000000000000000L选择的位)表示浮点数的符号。 Bits 62-52 (the bits that are selected by the mask 0x7ff0000000000000L) represent the exponent. 位62-52(由掩码0x7ff0000000000000L选择的位)表示指数。 Bits 51-0 (the bits that are selected by the mask 0x000fffffffffffffL) represent the significand (sometimes called the mantissa) of the floating-point number. 位51-0(由掩码0x000fffffffffffffL选择的位)表示浮点数的有效数(有时称为尾数)。

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. 如果你在双精度上做右移,那么signbit将被解释为指数,并且指数的一部分将溢出到有效数字。 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: 如果你真的想转移这些位,你可以通过Double.doubleToLongBitsDouble.longBitsToDouble ,如下所示:

// Unsigned shift right 3 of a double
Double.longBitsToDouble(Double.doubleToLongBits(d) >>> 3)

From the Java Language Specification 来自Java语言规范

It is a compile-time error if the type of each of the operands of a shift operator, after unary numeric promotion, is not a primitive integral type. 如果在一元数字提升之后移位运算符的每个操作数的类型不是基本整数类型,则这是编译时错误。

You cannot apply any of the shift operators to floating point type primitive values. 您不能将任何移位运算符应用于浮点类型原始值。

Floating-point types are inherently signed, so >>>= would make little sense for float or double . 浮点类型本身是签名的,因此>>>=floatdouble float没有意义。 If you can't use / by a power of 2, then you have a big problem. 如果你不能使用/ 2的幂,那么你就有一个大问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM