What is a >>> operator?

In the filter documentation page on the Mozilla website, I saw the operator >>> :

 var t = Object(this), len = t.length >>> 0, //here res, thisp, i, val; if (typeof fun !== 'function') { throw new TypeError(); } 

Here you can find the full document: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

What is this operator and what is it doing?

+4
source share
3 answers

This is an operator with a slight shift.

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators ,

a >>> b shifts a in binary representation b bits to the right, discarding bits shifted down and shifting zeros to the left.

This does not explain why someone would bother to shift zero to the right. You can also multiply it by one or add zero.

+3
source

As others have explained, this is a bitwise zero shift operator.

For positive values, this has the same effect as the regular >> operator. With negative values, the most significant bit is the โ€œsignโ€ bit. A normal shift will shift the sign bit to (1 for negative values, 0 for positive). >>> has a different effect, since it always shifts to zero instead of the sign bit:

 -2>>1 == -1 -2>>>1 == 2147483647 

More information on how negative values โ€‹โ€‹are presented can be found here .

What all shift operators do is different from a 32-bit integer (at least my Firefox does), so going to 0 means that the value will always fall within the 32-bit range. A bit shift with 0 also ensures that the value is positive:

 a = Math.pow(2,32) // overflow in 32-bit integer a>>0 == 0 b = Math.pow(2,32) - 1 // max 32-bit integer: -1 when signed, 4294967295 when unsigned b>>0 == -1 b>>>0 == 4294967295 // equal to Math.pow(2,32)-1 
+2
source

A bit offset to the right with a null operator is called. This operator is similar to the โ†’ operator, except that the bit shifted to the left is always zero.

For example: (A โ†’> 1) is 1.

http://www.tutorialspoint.com/javascript/javascript_operators.htm

Update: This explains what the bitwise shift operators do: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_shift_operators

0
source

Source: https://habr.com/ru/post/1495909/


All Articles