所以我在控制台中尝试着切换,结果让我难住了。
输入:
a = -1
a >>> 100
输出:
268435455
我查看了Mozilla reference page about it ,但它没有提到当你大量移动时 >>> 的行为。我假设用零填充将所有位向右移动将导致零。
这是 Firefox 中的错误还是什么?
最佳答案
看来您最多只能移动 31。
来自您在帖子中链接的网站 ( MDN ):
Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off, and shifting in zeros from the left.
来自实际规范 ( Page 77 )
Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.
实际发生的情况是,当您移动 100 时,它会移动 (100 & 0x1F)
或 4。
-1 >>> 100 === -1 >>> 4
如果您将其分成多个类次,那么它会起作用:
-1 >>> 25 >>> 25 >>> 25 >>> 25 === 0
关于javascript - 为什么 JavaScript 中 >>> 100 不会产生 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19825435/