1

Looking at a line of code

if(display & (1<<2))

what does 1<<2 mean?

And am I right in thinking that & is bitwise AND operator?

Oli
  • 39
  • 1
  • 1
  • 6
  • 7
    You can always [check the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators). – ajp15243 Sep 23 '13 at 15:55

1 Answers1

7

It's the bitwise left shift operator. The operands are converted to 32-bit integers, the left operand's bits are shifted left by the number of positions defined by the right operand, and the expression's value is the result.

Here's a simple example:

var a = 1;
var b = a << 2; // Move the bit left by two places
console.log(b); // "4"

That works because in a signed 32-bit integer, 1 looks like this in binary:

00000001

If you move that bit to the left two places:

00000100

...you get 4.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875