views:

252

answers:

4

What does the following Javascript statement do to a?

a >>>= b;
+9  A: 

It does the same thing as this:

a = a >>> b;

Except that a is only evaluated once (which has observable difference if its evaluation involves any side effects).

And >>> is unsigned (logical) right shift.

Pavel Minaev
Thank you! This is some crazy complex encoding/decoding code I am looking at here.
Josh Stodola
`a` is only "evaluated once" for `a = a >>> b` too. It's not like there is a second time where it is looked up. Both expressions return the expression's return value, not `a` (which was set to the value).
Eli Grey
@Elijah: the assumption is that `a` is a placeholder for an arbitrary expression, not just an identifier.
Pavel Minaev
+2  A: 

It's a bitwise operator called zero-fill right shift. It will shift the binary representation of a to the right by b places, and replace the empty items with zeros. Then the result will be assigned to a.

JacobM
+5  A: 

I right shifts the value in a the number of bits specified by the value in b, without maintaining the sign.

It's like the >>= operator that rights shifts a value, only that one does not change the sign of the number.

Example:

var a = -1;

// a now contains -1, or 11111111 11111111 11111111 11111111 binary

var b = 1;
a >>>= b;

// a now contains 2147483647, or 01111111 11111111 11111111 11111111 binary.
Guffa
Thank you for providing that example!
Josh Stodola
Holy crap, this is some complex stuff! o_0
KyleFarris
A: 

Crockford points out that while JavaScript has bitwise operators like >>>, using them on its native double-precision floating point numbers implies converting back and forth to integers internally. They will not be as efficient as in other languages with native integer data types.

ewg