Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ">>>" operator in JS? [duplicate]

Possible Duplicate:
javascript >>> operator?
JavaScript triple greater than

Found this operator in such line of code:

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

What does this operator mean?

Full code is below. It is the code of JS some method:

if (!Array.prototype.some) {   Array.prototype.some = function(fun /*, thisp */) {     "use strict";      if (this == null) throw new TypeError();      var t = Object(this),         len = t.length >>> 0;      if (typeof fun != "function") throw new TypeError();      var thisp = arguments[1];      for (var i = 0; i < len; i++) {       if (i in t && fun.call(thisp, t[i], i, t))         return true;     }      return false;   }; } 
like image 442
Green Avatar asked Apr 30 '12 10:04

Green


People also ask

What does >>> mean in JavaScript?

The unsigned right shift operator ( >>> ) (zero-fill right shift) evaluates the left-hand operand as an unsigned number, and shifts the binary representation of that number by the number of bits, modulo 32, specified by the right-hand operand.

What is the use of >>> operator?

Object Oriented Programming Fundamentals The >> operator is a signed right shift operator and >>> is an unsigned right shift operator. The left operands value is moved right by the number of bits specified by the right operand.

What is the difference between >> and >>> operators in JavaScript?

>> is arithmetic shift right, >>> is logical shift right. In an arithmetic shift, the sign bit is extended to preserve the signedness of the number.

What is the difference between the >> and >>> operators explain with the help of a program?

Difference between >> and >>> operator. Both >> and >>> are used to shift the bits towards the right. The difference is that the >> preserve the sign bit while the operator >>> does not preserve the sign bit. To preserve the sign bit, you need to add 0 in the MSB.


1 Answers

>>> is a right shift without sign extension

If you use the >> operator on a negative number, the result will also be negative because the original sign bit is copied into all of the new bits. With >>> a zero will be copied in instead.

In this particular case it's just being used as a way to restrict the length field to an unsigned 31 bit integer, or in other words to "cast" Javascript's native IEEE754 "double" number into an integer.

like image 85
Alnitak Avatar answered Oct 11 '22 13:10

Alnitak