为什么移位运算符 >> 中负数的结果与正常除法不同?

why the result of a negative number in shift operator >> is different from the normal division?

我想了解移位运算符的概念, 我正在测试以下代码:

15 >> 2;    
- 15 >> 2 

第一条语句的结果是 3,因为 15/4=3 但是第二条语句的结果是 -4.

我知道-15的二进制是11110001,右移2是11111100,这是-4。但我不明白为什么结果与简单除法 -15/4= -3 而不是 -4 不同?

请指导我为什么会这样?

移位运算符通过发言而不是截断来删除任何数学分数。 JLS, Section 15.19,声明:

The value of n >> s is n right-shifted s bit positions with sign-extension. The resulting value is floor(n / 2<sup>s</sup>). For non-negative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s.

-3.75 的下限是 -4 而截断会产生 -3

当值右移时,位会在值移位 "off the end" 时丢失。这是负责楼层操作的。

-15: 11110001
 -4: 11111100 // The rightmost 1 bit above is lost, resulting in what looks like the floor function.

Java用two's complement表示负数。
将带符号的二进制补码右移 n 位具有将其除以 2n 的效果,但它总是向下舍入(朝向负无穷大)。这不同于通常在有符号整数除法(向 0 舍入)中完成的舍入方式。