Strange javascript operator: expr >>> 0 [duplicate]

风格不统一 提交于 2019-11-30 03:03:31
Brandon McKinney

Source: LINK

This is the zero-fill right shift operator which shifts the binary representation of the first operand to the right by the number of places specified by the second operand. Bits shifted off to the right are discarded and zeroes are added on to the left. With a positive number you would get the same result as with the sign-propagating right shift operator, but negative numbers lose their sign becoming positive as in the next example, which (assuming 'a' to be -13) would return 1073741820:

Code:

result = a >>> b;

>>> is the Zero-fill right shift operator. The >>> 0 is an abuse of the operator to convert any numeric expression to an "integer" or non-numeric expression to zero. Here is what it does:

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes 0, so the result is always positive.

Here is an explanation of the convert-to-integer behavior which applies to all bitwise operations:

Bitwise operators treat their operands as a sequence of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. [...] Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.

Together, these statements assert that expr >>> 0 will always return a positive number as follows:

  1. expr is cast to a 32-bit integer for bitwise operation
  2. >>> 0 has no effect (no bits are shifted)
  3. The result is converted to a Number

Here are a few expressions and their outcome:

        1 >>> 0 // 1 -- Number cast to 32-bit integer then back to Number
      "1" >>> 0 // 1 -- String cast to 32-bit integer then back to Number
undefined >>> 0 // 0 -- failed cast yields zero

Other interesting cases:

      1.1 >>> 0 // 1          -- decimal portion gets it
       -1 >>> 0 // 4294967295 -- -1 = 0xFFFFFFFF
                //               Number(0xFFFFFFFF) = 4294967295
      "A" >>> 0 // 0          -- cast failed
    "1e2" >>> 0 // 100        -- 1x10^2 is 100
   "1e10" >>> 0 // 1410065408 -- 1x10^10 is 10000000000
                //               10000000000 is 0x00000002540BE400
                //               32 bits of that number is 0x540BE400
                //               Number(0x540BE400) is 1410065408

Note: you will notice that none of them return NaN.

The >>> (right-shift) binary operator is simply shifting the right-most bits of a number a specified number of times, and padding with zeroes to the left.

Note: In the following examples, the number in braces after a number signals what base it's in. 2 is for binary, 10 for decimal.

For example, 4 >>> 1 would do:

4(10) = 100(2)

4(10) >>> 1(10) = 010(2) = 2(10)
        shift once to the right

Other examples:

4(10) >>> 2(10) = 100(2) >>> 2(10) = 001(2) = 1(10)

10(10) >>> 4(10) = 1010(2) >>> 4(10) = 0000(2) = 0(10)

15(10) >>> 1(10) = 1111(2) >>> 1(10) = 0111(2) = 7

The way I remember it is to move the necessary amount of bits to the right, and then write the number. Like, in the last example, I simply moved everything to the right once, so the result is 0111.

Shifting 0 times does...nothing. No idea why it's there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!