Nick Brunt
Nick Brunt

Reputation: 10057

What does >>= mean in JavaScript?

I found this in the dojo.js library:

13444: color >>= bits;

Context:

ArrayUtil.forEach(["b", "g", "r"], function(x){
  var c = color & mask;
  color >>= bits;
  t[x] = bits == 4 ? 17 * c : c;
});

I can't find any reference to it anywhere else. It's not in the O'Reilly JavaScript pocket reference or the Wikipedia page.

I know what it means in functional programming, but I'm pretty sure JavaScript doesn't support monads!

Upvotes: 2

Views: 227

Answers (6)

Ed Heal
Ed Heal

Reputation: 60007

It is the same a color = color >> bits - similar to operators like +=, -=, *= ...

EDIT

The >> (in the integer context) shifts bits to the right, i.e. dividing by 2 but keeping the sign bit in the same place

Upvotes: 6

Sarfraz
Sarfraz

Reputation: 382696

It is Right Shift operator. For example, the a >> b operator is actually same as a/2b.

In your case, it is equal to: color = color >> bits where color >> bits stands for color/2bits

As you can see, it divides first operand with 2 raised power of second operand eg 2bits; whatever is the value of bits there.


You can read more about it at MDN.

Upvotes: 2

zaf
zaf

Reputation: 23244

x >>= y

Is the same as

x = x >> y

Heres the reference that you need: Assignment Operators doc from mozilla

And also "Rare Javascript Operators".

Upvotes: 1

Phillip Ngan
Phillip Ngan

Reputation: 16106

Shift the bits in color right by "bits" binary digits.

Upvotes: 0

Matjaz Muhic
Matjaz Muhic

Reputation: 5588

Those are bitwise operators. The ">>" practically shifts bits (in binary) to the right. So you if you have "1010", then applying ">>" operator will return "0101".

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators

http://www.phpied.com/bitwise-operations-in-javascript/

Upvotes: 0

danwellman
danwellman

Reputation: 9253

I believe it is a bit shift assignment operator, there is some brief info here

Upvotes: 0

Related Questions