30

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;
  };
}
Community
  • 1
  • 1
Green
  • 28,742
  • 61
  • 158
  • 247
  • 4
    Have a look at: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators#.3E.3E.3E_%28Zero-fill_right_shift%29 – Felix Kling Apr 30 '12 at 10:28
  • Google is your friend. https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators – JB Nizet Apr 30 '12 at 10:29
  • 4
    JB Nizet: Thank you for reminding. But Google didn't show any results for "what is operator >>>" search. And this site too. They both said "no results". – Green Apr 30 '12 at 10:32

2 Answers2

49

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

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 1
    yup, it's a shame the question is closed - the context of _why_ this particular operator is used here is more important than the detail of its generic usage. – Alnitak Apr 30 '12 at 10:35
  • worth to mention, that there is nothing special here in the *signed right shift* operator. Anytime a *bitwise operator* is applied in ecmascript, values get converted into their 32bit version. So using `~~t.length` here would also have the same outcome. – jAndy Apr 30 '12 at 10:36
  • @jAndy actually it wouldn't - `~~n` still produces a negative result for a negative input. – Alnitak Apr 30 '12 at 10:37
  • @Alnitak: you're right. doh, anyway if for some reason a negative number gets converted with `>>> 0` there, the outcome would be a *pretty* huge number. That is desired? However, my above statement remains true for the *32 bit conversion*. – jAndy Apr 30 '12 at 10:39
  • `-1 >>> 0` gives `4294967295`, isn't that bad for a later iteration? (as in OP's script, `; i < len; i++`) – CodeManX Nov 25 '14 at 21:32
5

It's a zero-fill right shift. When you bit-shift a number, you can either decide to fill the left-most bits with zeros or with the sign bit.

In a two's complement number representation, negative numbers have a 1 as the leading bit whereas positive numbers have a 0. Thus if you don't "sign extend" (filling with zeros instead) and shift a negative number, it will result in a positive number.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • after reading the explanations - still little confused as I don't get much exposure to bitwise scenarios - in simple terms in the code example - was this done to _prevent_ a negative number? I got lost in your last sentence. thx – jamie Oct 22 '15 at 18:17