gpt4 book ai didi

javascript ->> 0 和 >>> 0 的目的

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:58:04 26 4
gpt4 key购买 nike

This sample code from Mozilla's JS reference使用带有 RHS 参数 0 的 >> 和 >>> 运算符。我想这是 Math.floor() 的替代方法,它具有性能优势,因为它使用内置运算符而不必查找函数。但是 >> 与 >>> 零偏移有什么区别呢?

最佳答案

来自MDN Article :

>>> 是一个 Sign-propagating right shift :

Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off.

>>> 是一个 Zero-fill right shift :

Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off, and shifting in zeroes from the left.

所以不同之处在于,一个将从左边移入零。

来自 this stackoverflow answer谈论零偏移:

So doing a bitwise operation with no actual effect, like a rightward-shift of 0 bits >>0, is a quick way to round a number and ensure it is in the 32-bit int range. Additionally, the triple >>> operator, after doing its unsigned operation, converts the results of its calculation to Number as an unsigned integer rather than the signed integer the others do, so it can be used to convert negatives to the 32-bit-two's-complement version as a large Number. Using >>>0 ensures you've got an integer between 0 and 0xFFFFFFFF.

关于javascript ->> 0 和 >>> 0 的目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34796972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com