Java 轮班操作员和自动升级

Java Shift Operator and auto promotion

我是新手 Java 程序员,正在阅读 Bruce Eckel 的 Thinking In Java

在第5章中,已经讨论了运算符。在谈论移位运算符(<<、>>、>>>)的地方,他说:

If you shift a char, byte, or short, it will be promoted to int before the shift takes place, and the result will be an int. Only the five low-order bits of the right-hand side will be used. This prevents you from shifting more than the number of bits in an int. If you’re operating on a long, you’ll get a long result. Only the six low-order bits of the right-hand side will be used, so you can’t shift more than the number of bits in a long.

我看不懂什么意思。特别是大胆的句子。你能解释一下吗?

他指的是从你要移动的数字中使用的位数,所以对于一个 int,"five low-order bits of the right-hand side" 给你 5 位 = 2 ^ 5 = 32 位,你可以将 int 移位。类似地,六位 = 2 ^ 6 = 64,所以你只能将 long 移位 64 位。请注意,这些数字分别对应于 int 和 long 的大小。

移位运算的结果被解释为 intlong,具体取决于 左侧

int(32位)当左侧为int或更低时(8位byte、16位char、16位short )

long(64位)当左边为long

为了将结果限制在这些范围内,右侧 必须按以下方式限制:

Only the five low-order bits of the right-hand side will be used.

不允许轮班超过 31 (%0001 1111)

Only the six low-order bits of the right-hand side will be used.

不允许轮班超过 63 (%0011 1111)

如果您使用的数字大于可接受的数字,结果将仅使用较低的位(更靠右)有效地执行 32 或 64 的模运算。

感觉其他回答有点不完整

确实 int 是 32 位,并且该语言不允许您移动超过 32 位。遗漏的是,如果你告诉它移位超过 32 位,移位量将取 modulo 32。也就是说,如果 x 是一个 intx >> 32x >> 0 相同(即只是 x),x >> 33x >> 1 相同,x >> 66 与 [=19= 相同], 等等。仅使用右参数的低 5 位与采用参数 modulo 32 相同(使用数学定义,因此 -1 mod 32 == 31, -2 mod 32 == 30 等)。

类似地,long是64位,这意味着右参数被计算modulo 64,这与只使用右参数的低6位是一样的。

为什么Java这样做,我不知道。在我看来,如果它只是让你大量移动,将所有位从整数中移出并得到 0(除了 >> 符号扩展,所以结果将是 0,它会更一致或 -1)。它在数学上是一致的,因为对于正 xyx >> y 将始终等于 x / (2^y) 截断为整数,即使 y > = 32。Java 处理 >= 32 或 >= 64 的偏移量的方式在我看来毫无用处。