java - 为什么 -2>>>1 在 Java 中等于 2147483647

标签 java operator-keyword twos-complement ones-complement

-2的补码是100000...01

-2 的补码是 1000000...10

-2 >>> 1 

根据 >>> 定义左移 0

应该是01000......1,为什么变成了0111111..11

最佳答案

为了生成 2 的二进制补码表示(即 -2 的表示),您从 2 的表示开始,翻转它的所有位,并将 1 添加到结果中:

00000000000000000000000000000010 -- This is 2
11111111111111111111111111111101 -- This the inverse of 2
11111111111111111111111111111110 -- This is the inverse of 2, plus 1

-2 的二进制表示是 1111111111111111111111111111110 ( demo )。

不带符号扩展将其右移一位产生

01111111111111111111111111111111

这正是您得到的结果。

关于java - 为什么 -2>>>1 在 Java 中等于 2147483647,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35214602/

相关文章:

java - Java 的位移运算符在底层是如何工作的?

algorithm - 如何在将两个2的补码整数相乘时检测到溢出?

java - Spring AOP : how to get the annotations of the adviced method

python - 在 python 中重载 * 运算符(或模拟它)

c++ - 向 C++ 中的结构数组添加元素遇到 "error no match for ‘operator=’"

c - C 中数据类型的二进制表示

java - 如何使用java在selenium中将结果输入到excel文件中

java - 在 Java 中按特定顺序连接 ArrayList

java - 从 Java 可见的 Scala 中的包私有(private)范围