Hack to convert javascript number to UInt32

前端 未结 3 1304
半阙折子戏
半阙折子戏 2021-02-15 01:58

Edit: This question is out of date as the Polyfill example has been updated. I\'m leaving the question here just for reference. Read the correct answer for usef

3条回答
  •  孤独总比滥情好
    2021-02-15 02:24

    The ECMAScript specification states that the value is converted to UInt32 in step 5 and 8 of http://www.ecma-international.org/ecma-262/5.1/#sec-11.7:

    11.7.3 The Unsigned Right Shift Operator ( >>> )

    Performs a zero-filling bitwise right shift operation on the left operand by the amount > specified by the right operand.

    The production ShiftExpression : ShiftExpression >>> AdditiveExpression is evaluated as follows:

    1. Let lref be the result of evaluating ShiftExpression.
    2. Let lval be GetValue(lref).
    3. Let rref be the result of evaluating AdditiveExpression.
    4. Let rval be GetValue(rref).
    5. Let lnum be ToUint32(lval).
    6. Let rnum be ToUint32(rval).
    7. Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.
    8. Return the result of performing a zero-filling right shift of lnum by shiftCount bits. Vacated bits are filled with zero. The result is an unsigned 32-bit integer.

提交回复
热议问题