意外的右移运算符行为

Unexpected Right Shift Operator behavior

请查看以下Node.js REPL 输出:

> n=10
10
> console.log(n.toString(2)); //ok ?
1010
undefined
> m=n>>1 
5
> console.log(m.toString(2)); //cool :)
101

这是按预期工作的。现在看看这个:

> n=Number.MAX_VALUE
1.7976931348623157e+308
> m=n>>1
0
> n.toString(16) // to read easier
'fffffffffffff800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
> m.toString(16) // to read easier
'0'

这里刚刚发生了什么?我不应该右移

fffffffffffff800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

1位给我

7ffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

我是怎么得到零的?

此外,我注意到这不仅仅是 V8/Node 问题。我在 Firebug.

中得到相同的结果

Javascript 的位级操作仅适用于 32 位有符号值(或在零扩展 >>> 运算符的情况下无符号)。在位操作发生之前,高于这 32 位的任何内容都设置为零。

在JavaScript中,当你计算x >> y时,x被转换成一个带符号的32位整数。然后右移 y 位。

Your value 0xfffffffffffff800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 is converted into 0x00000000 because those are the lowest 32 bits.右移这得到零。