>>" - what is it used for? I just looked through the Mozilla Developer documentation and found that I did not know what was being us...">

The operator ">>>" - what is it used for?

I just looked through the Mozilla Developer documentation and found that I did not know what was being used and also could not find any information over the Internet.

Polyfill array filter - line 10

var t = Object(this); var len = t.length >>> 0; 

Any suggestions what this operator is for?

+5
source share
2 answers

TL DR

 t.length >>> 0; 

actually trying to get a valid 32-bit unsigned integer from t.length . For what we know, t.length can be of any type (object, array, string, etc.). >>> 0 returns an unchanged value if it is already a valid 32-bit unsigned number. For instance,

 console.log({} >>> 0); // 0 console.log([] >>> 0); // 0 console.log("Google" >>> 0); // 0 

Usually this bitwise trick is used to avoid checking the type of the if block, like this

 var len = 0; if (typeof data === 'number') { len = data; } 

We may need to convert len to an integer if this is a floating point value.

Explanation

>>> called the zero-shift operator. Besides being used as a bitwise operator, it is used to get a 32-bit numeric value from an object. ECMA Script 5.1 Specification for >>> says that

  • Let lref be the result of evaluating the Shift expression.
  • Let lval be GetValue (lref).
  • Let rref be the result of evaluating AdditiveExpression.
  • Let rval be GetValue (rref).
  • Let lnum be ToUint32 (lval).
  • Let rnum be ToUint32 (rval).
  • Let shiftCount be the result of masking all but the least significant 5 bits of rnum, i.e. rnum and 0x1F.
  • Returns the result of performing zero fill of the lnum shift on the shiftCount bits. Running bits are filled with zero. The result is a 32-bit unsigned integer.

It basically converts both operands to an unsigned 32-bit integer (steps 5 and 6) and shifts the left side expression, the time of the right side expression.

If we look at the definition of ToInt32 ,

  • Let the number be the result of calling ToNumber on the input argument.
  • If the number is NaN, +0, -0, + ∞ or -∞, return +0.
  • Let posInt be the sign (number) * gender (abs (number)).
  • Let int32bit be posInt modulo 2 32 ; those. a finite integer value k of type Number with a positive sign and less than 2 32 in magnitude, so that the mathematical difference of posInt and k is mathematically an integer multiple of 2 32 .
  • Returns int32bit.

First, the argument is converted to a number (if it is not a valid number, then NaN will be returned ToNumber ). And step 4 ensures that you return a real number in the range from 0 to 2 32 .

+4
source

This is aa Zero padding operation on the right (bitwise).

This operator shifts the first operand by the specified number of bits to the right. Erased bits shifted to the right are discarded. Zero bits are shifted to the left. The bit sign becomes 0 , so the result is always non-negative.

For non-negative numbers, the right shift of the zero fill and the shift to the right of the shift of the shift give the same result. For example, 9 >>> 2 gives 2 , the same as 9 >> 2 :

However, this does not apply to negative numbers. For example, -9 >>> 2 gives 1073741821 , which differs from -9 >> 2 (which gives -3 ):

@thefourtheye responded with a good explanation about using this operator.

+4
source

All Articles