What does the following Javascript statement do to a
?
a >>>= b;
What does the following Javascript statement do to a
?
a >>>= b;
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.
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
.
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.
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.