4

In filter documentation page in Mozilla website, I saw >>> operator:

var t = Object(this),
     len = t.length >>> 0, //here
     res, thisp, i, val;
if (typeof fun !== 'function') {
    throw new TypeError();
}

Here you can find the complete document: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

What's this operator and what it does?

Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
  • It's a bitwise right shift operator. http://stackoverflow.com/q/3081987 – Jonathan Lonowski Aug 08 '13 at 09:00
  • 2
    MDN has a good [index of JavaScript operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators). Should help in finding any since they can be rather peculiar to search for. – Jonathan Lonowski Aug 08 '13 at 09:02
  • 1
    None of the answers here or in the dupe cover the special (and the most important) case `>>>0`. – georg Aug 08 '13 at 09:25

3 Answers3

3

It's a bit shift operator.

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators,

a >>> b shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.

That doesn't explain why anyone would bother to shift a value zero bits to the right, though. You might as well multiply it by one, or add zero.

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
2

As others explained, it is the "bitwise shift with zero" operator.

With positive values this has the same effect as the normal >> operator. With negative values, the most-significant bit is the "sign" bit. Normal shifting will shift the sign bit in (1 for negative values, 0 for positive). >>> has a different effect, because it always shifts in a zero instead of the sign bit:

-2>>1 == -1
-2>>>1 == 2147483647

More on how negative values are represented can be found here.

What all shift operators do is cast the value to a 32-bit integer (at least my Firefox does), so shifting by 0 means that the value will always be within the 32-bit range. Bitwise shift with 0 will also make sure the value is positive:

a = Math.pow(2,32)       // overflow in 32-bit integer
a>>0 == 0
b = Math.pow(2,32) - 1   // max 32-bit integer: -1 when signed, 4294967295 when unsigned
b>>0 == -1
b>>>0 == 4294967295      // equal to Math.pow(2,32)-1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • so that the value comes to always positive? –  Aug 08 '13 at 09:21
  • @rps: Yes, the result would always be positive. This isn't the core intention of the operator, but it is an effect of it as long as your numeric sign bit is stored as `1` in the MSB. – Lightness Races in Orbit Aug 08 '13 at 09:23
  • +1 for your explanation of the difference between the two, but this also raises the question, when would you want to use a `>>>` operator in place of a `>>` operator? My guess is that it's slightly cheaper, and if you're using it for number crunching when you're confident that your numbers will stay positive, you make a small saving by using `>>>`. – Rob Lyndon Aug 08 '13 at 09:25
  • Also it seems to generate a very larger positive number! is that also a notable effect of it? I have just now read that it is used by mozilla to make sure that array length is non-negative and within 2 power 32 –  Aug 08 '13 at 09:29
0

Called Bitwise Shift Right with Zero Operator. This operator is just like the >> operator, except that the bits shifted in on the left are always zero.

For example: (A >>> 1) is 1.

http://www.tutorialspoint.com/javascript/javascript_operators.htm

Update: This explains what bitwise shift operators do: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_shift_operators

Mentatmatt
  • 515
  • 5
  • 13
  • 2
    This doesn't explain much unless you already know what the >> operator does. Or what the value of `A` is. – JJJ Aug 08 '13 at 09:02
  • 1
    `a>>1` is also `1`. The difference is visible when shifting negative values: `-2>>1 = -1`, but `-2>>>1 = 2147483647`. – Bart Friederichs Aug 08 '13 at 09:05