Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange javascript operator: expr >>> 0 [duplicate]

the following function is designed to implement the indexOf property in IE. If you've ever had to do this, I'm sure you've seen it before.

if (!Array.prototype.indexOf){    Array.prototype.indexOf = function(elt, from){      var len = this.length >>> 0;     var from = Number(arguments[1]) || 0;      from = (from < 0)          ? Math.ceil(from)          : Math.floor(from);      if (from < 0)       from += len;      for (; from < len; from++){       if (from in this &&               this[from] === elt)         return from;     }      return -1;       }; } 

I'm wondering if it's common to use three greater than signs as the author has done in the initial length check?

var len = this.length >>> 0

Doing this in a console simply returns the length of the object I pass to it, not true or false, which left me pondering the purpose of the syntax. Is this some high-level JavaScript Ninja technique that I don't know about? If so, please enlighten me!

like image 845
Hacknightly Avatar asked Apr 21 '11 16:04

Hacknightly


Video Answer


2 Answers

>>> is the Zero-fill right shift operator. The >>> 0 is an abuse of the operator to convert any numeric expression to an "integer" or non-numeric expression to zero. Here is what it does:

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 positive.

Here is an explanation of the convert-to-integer behavior which applies to all bitwise operations:

Bitwise operators treat their operands as a sequence of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. [...] Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.

Together, these statements assert that expr >>> 0 will always return a positive number as follows:

  1. expr is cast to a 32-bit integer for bitwise operation
  2. >>> 0 has no effect (no bits are shifted)
  3. The result is converted to a Number

Here are a few expressions and their outcome:

        1 >>> 0 // 1 -- Number cast to 32-bit integer then back to Number       "1" >>> 0 // 1 -- String cast to 32-bit integer then back to Number undefined >>> 0 // 0 -- failed cast yields zero 

Other interesting cases:

      1.1 >>> 0 // 1          -- decimal portion gets it        -1 >>> 0 // 4294967295 -- -1 = 0xFFFFFFFF                 //               Number(0xFFFFFFFF) = 4294967295       "A" >>> 0 // 0          -- cast failed     "1e2" >>> 0 // 100        -- 1x10^2 is 100    "1e10" >>> 0 // 1410065408 -- 1x10^10 is 10000000000                 //               10000000000 is 0x00000002540BE400                 //               32 bits of that number is 0x540BE400                 //               Number(0x540BE400) is 1410065408 

Note: you will notice that none of them return NaN.

like image 174
Salman A Avatar answered Sep 18 '22 06:09

Salman A


Source: LINK

This is the zero-fill right shift operator which shifts the binary representation of the first operand to the right by the number of places specified by the second operand. Bits shifted off to the right are discarded and zeroes are added on to the left. With a positive number you would get the same result as with the sign-propagating right shift operator, but negative numbers lose their sign becoming positive as in the next example, which (assuming 'a' to be -13) would return 1073741820:

Code:

result = a >>> b; 
like image 43
Brandon McKinney Avatar answered Sep 21 '22 06:09

Brandon McKinney