請看看下面的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中得到了相同的結果。
我想你想['Number.MAX_SAFE_INTEGER'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) –
@HunterMcMillen居然,不,因爲那個範圍是53位,但是位操作只能工作在32位 – Alnitak