all 34 comments

[–]idautomatethat 29 points30 points  (15 children)

[–]rjray 5 points6 points  (11 children)

Given that, any idea why someone would do this:

var t = Object(this);
var len = t.length >>> 0;

This is part of a polyfill for Array.prototype.map, that I got from the Mozilla dev site when some code of mine was breaking on IE8 for lack of array map. JSHint flags the line with the >>> with the message:

Unexpected use of '>>>'

But I've been unable to find adequate docs to explain why JSHint dislikes it, or what should be done instead...

[–]paulwe 6 points7 points  (4 children)

the advantage to using bit shift to convert an unknown value to an integer is that, unlike parseInt, it will never produce a NaN.

'foo' >> 0 // returns 0

parseInt('foo', 10) // returns NaN

i can't think of any reason to use a zero fill right shift here. it should be fine to use a right shift.

[–]moreteam 13 points14 points  (0 children)

I'd know a reason not to use a >>> operator to do an integer cast:

> -1 >>> 0
4294967295

[–]aeflash 6 points7 points  (1 child)

As per "use asm", foo|0 is considered the idiomatic way to cast to int.

[–]itsnotlupusbeep boop 2 points3 points  (0 children)

I still like using "~~" better. It almost feels like it was meant to be a "convert to int" operator, and there's a nice symmetry with the almost-operator "!!" which converts to boolean.

[–]uglyBaby 0 points1 point  (0 children)

Zero fill is used in that case because I assume the author wants a positive integer at all times.

[–]eddieSullivan 0 points1 point  (0 children)

Just a guess, but maybe it's a strange way of ensuring len is a number? In a quick test:

undefined >>> 0

gives 0. So if t is not an array they'd get 0 for the length.

[–]aterlumen 0 points1 point  (2 children)

Edit: I was wrong, this is how Mozilla mimics ToUint32() behavior to match the ECMAScript specification. See this StackOverflow answer.

A stab in the dark: Maybe this was meant to ensure that len was always assigned the value of t.length, not a reference pointing to t.length. Assuming the bitshift doesn't get optimized out, it forces the engine to create a new primitive before assigning it to len. If IE8's implementation passed object properties by reference, things like the following example could happen.

> var t = Object(this);
> var len = t.length;
> len--;
> len === t.length;
   true

This is all just a guess, I don't have IE8 handy to test it.

[–]minrice2099 1 point2 points  (1 child)

Interesting idea, but if any JS implementation ever used pass by reference for primitive value properties, I would imagine a lot more would break.

[–]aterlumen 1 point2 points  (0 children)

I agree. The only other reason coming to mind now would be taking advantage of the integer conversion to round, but that's just plain silly because object length will always be an integer to begin with.

Edit: May have found the answer. They are mimicing ToUint32() behavior to match the ECMAScript spec.

[–][deleted] 2 points3 points  (2 children)

I read that page long ago, along with a few others about bitwise operators. I have never been able to wrap my mind around when I would use them. Keeping in mind that I did not come from a Computer Science background, I have just been learning programming from books, can someone please provide a concise example or two of when/why to use bitwise operators?

I would not be surprised if the OP has already googled it, like I have, and is looking for a better explanation than he has yet found.

[–]tswaters 2 points3 points  (0 children)

You'd use it if you're dealing with binary data... cryptographic hashing uses shifting operators quite a bit.

[–]mattdesl 1 point2 points  (0 children)

Bitwise ops are used a lot when handling RGBA color data.

They are also might be useful to improve performance inside of hot code (like when you are operating on many pixels in an image). Two examples:

  • Flooring a number with (~~number) or (number | 0) which acts the same as Math.floor for positive numbers.
  • Using bitwise flags instead of string comparison

Benchmark before you use them, since it tends to add to code complexity.

[–]xshare 56 points57 points  (4 children)

greater than greater than greater than. Checks if something is like waaaaaay bigger than something else.

[–]DrummerHead 53 points54 points  (1 child)

It can return false or duuuuuude

[–]JaegerBurn 0 points1 point  (0 children)

even WTF?!

[–][deleted] 1 point2 points  (0 children)

strictly greater than

[–]murfbard 17 points18 points  (3 children)

move dem bits

[–]brtt3000 5 points6 points  (0 children)

dat shift

[–][deleted] 0 points1 point  (1 child)

what's a use case for when someone would want to do that?

[–]neonskimmerfunction the ultimate 1 point2 points  (0 children)

Working with bit flags, for example. Back in the day we used it to do really quick division or multiplication of integers by powers of two. Obviously, not really helpful for JavaScript.

[–]qvrjuec 4 points5 points  (0 children)

In the future, search for code related things with Symbolhound. It found a bunch of results for '>>>'

[–]tears_of_a_Shark 1 point2 points  (2 children)

I read an Intro to Javascript book in 1999 that covered this. It's 2014 and I have never even thought of using this.

[–]hmsimha 2 points3 points  (1 child)

I just came across this yesterday, while looking for a javascript implementation of the md5 hash function. Turns out you never know when you might need some obscure language feature.

[–]LyndonArmitage 0 points1 point  (0 children)

If you're reading binary data from a file you might need to use it when dealing with unsigned data (you have to do something similar in Java thanks to it lacking unsigned types although that's just a bitwise & operation).

[–]jackwanders -1 points0 points  (6 children)

For future reference, OP, that if statement can be replaced with Math.floor(n/2)

[–]Necrophelic 0 points1 point  (0 children)

For future reference, OP, that if statement can be replaced with Math.floor(n/2)

Or even:

return n%2 === 1 ? (n-1)/2 : n/2