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;
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.
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.
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.
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.
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 };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With