在 Node 环境 | JavaScript 代码:
1 << 33 === 1 << 1
// true
我知道数字是以 32 位存储的。它在
0000 0000 0000 0000 0000 0000 0000 0001
之后:<< 33
1 0000 0000 0000 0000 0000 0000 0000 000?
和:
1<< 35 === 1 << 3
问题:<<
怎么办?运算符(operator)在这里工作?
最佳答案
1) << operators how to work?
您可以找到 <<
的定义within the specification (ECMA 262)。
你的问题的关键步骤是:
11) Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum
& 0x1F
.
这意味着第二个操作数不能大于 31
(0x1F
)。如果是,则在继续之前通过位掩码减少它:
(33 & 0x1f) === 1 // true
所以:
1 << 33 // becomes...
1 << (33 & 0x1f) // becomes...
1 << 1
2) How is Numbers in the memory to stored?
A number in JavaScript是一个:
primitive value corresponding to a double-precision 64-bit binary format IEEE 754-2008 value
关于javascript - << 运算符对大于 32 的移位有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36275040/