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

sth*_*der 3 java twos-complement operator-keyword ones-complement

-2一个人的补充是100000 ... 01

-2两个补码是1000000 ...... 10

-2 >>> 1 
Run Code Online (Sandbox Code Playgroud)

根据>>>定义,左侧移位为0

应该是这样的 01000......1,为什么变成0111111..11什么?

das*_*ght 10

为了产生两个补码表示2(即-2表示),你从表示开始2,翻转它的所有位,并添加1到结果中:

00000000000000000000000000000010 -- This is 2
11111111111111111111111111111101 -- This the inverse of 2
11111111111111111111111111111110 -- This is the inverse of 2, plus 1
Run Code Online (Sandbox Code Playgroud)

-2的二进制表示是11111111111111111111111111111110(演示).

将其向右移动一个没有符号扩展的产生

01111111111111111111111111111111
Run Code Online (Sandbox Code Playgroud)

这正是你得到的结果.