5

Possible Duplicates:
1. What good does zero-fill bit-shifting by 0 do? (a >>> 0)
2. JavaScript triple greater than

I was digging through some MooTools code, and noticed this snippet being used in every array method:

var length = this.length >>> 0;

What's the benefit of doing this? It doesn't seem to me like it's some max length thing, as 3247823748372 >>> 0 === 828472596 and 3247823748373 >>> 0 === 828472597 (so, both are 1 higher, if it were for maxLength wouldn't it be better to do something like var length = Math.min(this.length, MAX_LENGTH)?).

Community
  • 1
  • 1
goto-bus-stop
  • 11,655
  • 2
  • 24
  • 31
  • 3
    See: http://stackoverflow.com/questions/7718711/javascript-triple-greater-than – NullUserException Nov 27 '11 at 16:16
  • Ah, I was wondering why I couldn't find anything but obviously search engines don't 'recognize' >s... Thanks for the link, that explains a lot ^^, – goto-bus-stop Nov 27 '11 at 16:18
  • @Reanimation: But were you asking for the reason/benefit of this approach compared to others? That duplicate doesn't seem to answer that part of your question. – RightSaidFred Nov 27 '11 at 16:20
  • I closed the question as it asks the same thing as the duplicate as it stands. But if you are wondering why it's being used on an integer (in which case `>>> 0` doesn't seem to make a lot of sense), feel free to edit the question and I'll reopen it. – NullUserException Nov 27 '11 at 16:20
  • @RightSaidFred: Actually it does, the second answer demonstrates how it converts to positive 32bit int which seems a logical thing to do on array lengths to be foolproof, some moron might set it to be '10k' ;) – goto-bus-stop Nov 27 '11 at 16:22
  • ... and the other duplicate link takes care of that part. – NullUserException Nov 27 '11 at 16:23
  • Ah, you mean the link that was added *after* you closed the question. Yes, that's a much better duplicate. Thankfully a member of the community found and added it. – RightSaidFred Nov 27 '11 at 16:31
  • I don't think that either duplicate question address why MooTools would use this operator on existing Array `length` properties. It is ensuring that the value returned by `length` is a positive 32-bit integer, and yet (as noted in the first duplicate) the specification already declares that the length property must be such. – Phrogz Nov 27 '11 at 16:34

2 Answers2

3

This seems to be the safest way to ensure length is a non-negative (32-bit) integer.

Just another example of where JavaScript lacks proper standard functions, this time for save conversion of unknown types into, well, 32-bit unsigned integers.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
2

>>> is the bitwise "zero-fill right shift" operator.

JavaScript numbers can represent both integers and floating point numbers. Sometimes you only want an integer. Any positive JavaScript Number representing a number less than 2^32 will be rounded down (truncated, as in Math.floor) to the nearest integer. Numbers ≥ 2^32 are turned to 0. Numbers less than 0 will turn into a positive value (thanks to the magic of two's-complement representation).

However, this.length would presumably ALWAYS be an integer less than 2^32…so I can't explain why the code would be doing that. The result should be the same as this.length.

Phrogz
  • 296,393
  • 112
  • 651
  • 745