Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was just looking through Mozilla Developer documentation, and found notation that I don't know what is used for and also cannot find any information through internet.

Array filter polyfill - line 10

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

Any suggestions what this operator is for ?

like image 522
kuba Avatar asked Jul 10 '15 13:07

kuba


2 Answers

TL;DR

t.length >>> 0;

actually tries to get a valid 32 bit unsigned integer from t.length. For what we know, t.length could be of any type (an object, array, string etc). >>> 0 returns the value unaltered if it is already a valid 32 bit unsigned number. For example,

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

Normally, this bitwise trick is used to avoid a if block type checking, like this

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

We might still have to convert len to an integer, if it is a floating point value.

Explanation

>>> is called Zero-fill right shift operator. Apart from being used as a bitwise operator, it is used to get a 32 bit numeric value out of an object. The ECMA Script 5.1 Specification for >>> says that

  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.

It basically converts both the operands to 32 bit unsigned integer (Step 5 and 6) and shifts the left hand side expression, right hand side expression times.

If we look at the definition of ToInt32,

  1. Let number be the result of calling ToNumber on the input argument.
  2. If number is NaN, +0, −0, +∞, or −∞, return +0.
  3. Let posInt be sign(number) * floor(abs(number)).
  4. Let int32bit be posInt modulo 232; that is, a finite integer value k of Number type with positive sign and less than 232 in magnitude such that the mathematical difference of posInt and k is mathematically an integer multiple of 232.
  5. Return int32bit.

First the argument is converted to a number (if it is a not a valid number then NaN will be returned by ToNumber). And step 4 makes sure that you return a valid number in the range 0 to 232.

like image 164
thefourtheye Avatar answered Nov 04 '22 06:11

thefourtheye


It's a "Zero-fill right shift" (bitwise) operator.

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 non-negative.

For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example, 9 >>> 2 yields 2, the same as 9 >> 2:

However, this is not the case for negative numbers. For example, -9 >>> 2 yields 1073741821, which is different than -9 >> 2 (which yields -3):

@thefourtheye answered with a good explanation about the usage of this operator.

like image 4
Cerbrus Avatar answered Nov 04 '22 06:11

Cerbrus