JavaScript Bitwise AND Operator Tutorial

From MDN:

quote: The bitwise AND operator (&) returns a 1 in each bit position for which the corresponding bits of both operands [numbers] are 1s. end quote

So, what does this mean in plain English? Let’s break it down with an example:

Take a look at the output of this node script:

2: 00000010
3: 00000011
===========
2: 00000010

Notice how every bit that is a 1 in both numbers becomes a 1 in the final result. But if neither bit is a 1 or only one of the bits in either number is a 1, the corresponding bit in the final result is a 0. That’s how the bitwise AND operator works. Let’s try another example:

5: 00000101
8: 00001000
===========

Given what you’ve just learned about bitwise AND, what should the result be? Use the node program above to confirm your answer.