Skip to content

Bitwise Operators & Binary Numbers

Bitwise operators work on the individual binary digits, or bits, of a number. Before applying one, JavaScript converts both operands to 32-bit signed integers, compares or moves the bits, and converts the result back to an ordinary number. They are useful for packing several on or off flags into one number, for reading parts of a colour value, and for a few quick arithmetic tricks.

Each binary digit is worth twice the one to its right, so from the right the columns are worth 1, 2, 4, 8, 16 and so on. The number 12 is 8 + 4, which is 1100 in binary, and 10 is 8 + 2, which is 1010. toString(2) shows any number in binary, and these are some to recognise:

Decimal Binary
1 1
2 10
3 11
4 100
8 1000
10 1010
12 1100
15 1111
16 10000

Number Bases covers binary, octal and hexadecimal in more detail.

Operator Name A bit in the result is 1 when
& AND both bits are 1
| OR either bit is 1, or both are
^ XOR (exclusive OR) exactly one of the bits is 1
~ NOT the bit was 0, because NOT flips every bit

Lining up 12 and 10 bit by bit shows each result:

1100 (12) 1100 (12) 1100 (12)
& 1010 (10) | 1010 (10) ^ 1010 (10)
---- ---- ----
1000 (8) 1110 (14) 0110 (6)

The same operations in code:

const a = 12; // 1100
const b = 10; // 1010
console.log(a.toString(2), b.toString(2));
console.log(a & b);
console.log(a | b);
console.log(a ^ b);
console.log(~a);

This prints:

1100 1010
8
14
6
-13

~a is -13 rather than a small positive number, because the flip also changes the sign bit of the 32-bit integer. For any whole number in range, ~x equals -(x + 1).

The rightmost bit of a whole number is 1 when the number is odd and 0 when it is even, so n & 1 gives the answer directly:

const pages = [3, 8, 15, 22];
for (const page of pages) {
console.log(page, (page & 1) === 1 ? 'odd' : 'even');
}

This prints:

3 odd
8 even
15 odd
22 even

n % 2 does the same job and is easier for most readers to follow, so the bitwise form is mainly worth recognising when it appears in other people’s code.

<< moves every bit to the left by the given number of places and fills the gap with zeros, which multiplies by 2 for each place. >> moves them to the right, dropping the bits that fall off the end, which divides by 2 for each place and rounds down. >> keeps the sign of the number, while >>> fills with zeros and so treats the number as unsigned:

console.log(5 << 1);
console.log(5 << 3);
console.log(40 >> 2);
console.log(-40 >> 2);
console.log(-40 >>> 2);

This prints:

10
40
10
-10
1073741814

A colour written as six hexadecimal digits, such as #2a9d8f, holds three 8-bit values: red, green and blue. Shifting the number right moves the channel you want to the bottom 8 bits, and & 0xff (255, eight 1s in binary) keeps only those bits:

const teal = 0x2a9d8f;
const red = (teal >> 16) & 0xff;
const green = (teal >> 8) & 0xff;
const blue = teal & 0xff;
console.log(red, green, blue);
console.log(green.toString(16));

This prints:

42 157 143
9d

Because the operands are converted to 32-bit integers, anything larger loses its upper bits and any fraction is dropped:

console.log(2 ** 32 | 0);
console.log(3_000_000_000 | 0);
console.log(7.9 | 0);

This prints:

0
-1294967296
7

x | 0 is sometimes used to drop a fraction, but it gives wrong answers for numbers outside the 32-bit range, so Math.trunc(x) is the safe choice. For bit operations on larger whole numbers, the same operators work on BigInt values, apart from >>>.

Permissions with Bitwise Operators puts AND, OR and XOR to work on a set of flags.

Steve Griffith covers this topic in his video JS Bitwise Operators and Binary Numbers.