首页 > 解决方案 > 左移丢失的信息是否存储在 Java 中的某个地方?

问题描述

所以我在 Java 中尝试左移,我发现了一些我不理解的东西。下面的代码描述了它。

int x = -1 << 31;
System.out.print(Integer.toBinaryString(-1) + " -1\n");
System.out.print(Integer.toBinaryString(x) + " " + x + "\n");

// output
11111111111111111111111111111111 -1
10000000000000000000000000000000 -2147483648

所以我只是去掉了整数-1左边的31个1,结果如我所愿。但是,在我尝试再左移 1 个位置后,我得到的不是 0,而是 -1。

int x = -1 << 32; // one more left shift
System.out.print(Integer.toBinaryString(-1) + " -1\n");
System.out.print(Integer.toBinaryString(x) + " " + x + "\n");

// output
11111111111111111111111111111111 -1
11111111111111111111111111111111 -1

然后我尝试将最小整数值 -2147483648 直接左移 1。我得到了预期值 0。

System.out.println(-2147483648 << 1); // the output is 0

有人可以向我解释幕后发生的事情吗?

标签: javabit-manipulationbitwise-operatorsbitbit-shift

解决方案


这是JLS 15.19

如果左侧操作数的提升类型是 int,则只有右侧操作数的五个最低位用作移位距离。就好像右手操作数受到按位逻辑与运算符 & (§15.22.1) 的影响,掩码值为 0x1f (0b11111)。因此,实际使用的移位距离始终在 0 到 31 的范围内,包括 0 到 31。

所以-1 << 32是一样的-1 << 0,解释你的结果。


推荐阅读