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?
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?
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
.