Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

>>> (Zero-fill right shift) Elaboration [duplicate]

Tags:

javascript

Possible Duplicate:
What good does zero-fill bit-shifting by 0 do? (a >>> 0)

I was looking at array.indexOf(), and I'm aware that IE7 does not natively support that. I was reading the MDC and saw their example of how to prototype it into browsers that don't support it. I'm reading through it trying to understand how everything works, but I'm not sure I'm understanding it 100%. The main cause for confusion is the bitwise operators, specifically >>>. I'm not sure what this operator is useful for. Below is the way that they're using it. Can anyone explain exactly what it's useful for and why you can't just if (t.length === 0)?

 var t = Object(this);  
 var len = t.length >>> 0;  
 if (len === 0)  
   return -1; 
like image 873
A Wizard Did It Avatar asked Nov 11 '10 03:11

A Wizard Did It


People also ask

What does the operator >>> do?

The right shift operator ( >> ) returns the signed number represented by the result of performing a sign-extending shift of the binary representation of the first operand (evaluated as a two's complement bit string) to the right by the number of bits, modulo 32, specified in the second operand.

Is zero fill right shift?

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 in Java?

The >>> operator is the unsigned right bit-shift operator in Java. It effectively divides the operand by 2 to the power of the right operand, or just 2 here.

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

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

It allows indexOf to be called on array-like objects that might have weird length properties.

For example:

var fakeArray = { length: -3, '0': true, '1': false, '2': null };
like image 131
SLaks Avatar answered Sep 23 '22 02:09

SLaks