Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does >>= mean in JavaScript?

Tags:

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!

like image 379
Nick Brunt Avatar asked Dec 30 '25 20:12

Nick Brunt


2 Answers

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

like image 81
Ed Heal Avatar answered Jan 01 '26 09:01

Ed Heal


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.

like image 25
Sarfraz Avatar answered Jan 01 '26 10:01

Sarfraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!