Sub topic 18
Bitwise and Shift Operators
This chapter assumes you already know the arithmetic operators and how == and === treat different types. Bitwise operators show up less often than the others, but they follow conversion rules that can catch you out. BigInt changes those rules again.
How Bitwise Operators See Numbers
JavaScript numbers are 64-bit floating-point values. A bitwise operator first converts each operand to a 32-bit signed integer using an internal step called ToInt32. It then works on the bits and turns the result back into a regular number.
| Operator | Name | Example | Result | Column | Column |
|---|---|---|---|---|---|
| `&` | AND | `5 & 3` | `1` | ||
| `\ | ` | OR | `5 \ | 3` | `7` |
| `^` | XOR | `5 ^ 3` | `6` | ||
| `~` | NOT | `~5` | `-6` | ||
| `<<` | Left shift | `1 << 3` | `8` | ||
| `>>` | Sign-propagating right shift | `-16 >> 2` | `-4` | ||
| `>>>` | Zero-fill right shift | `-1 >>> 0` | `4294967295` |
console.log(5 & 3); // 1 (0101 & 0011 = 0001)
console.log(5 | 3); // 7 (0101 | 0011 = 0111)
console.log(5 ^ 3); // 6 (0101 ^ 0011 = 0110)
console.log(~5); // -6 (~x is always -(x + 1))
console.log(1 << 3); // 8
console.log(-16 >> 2); // -4: the sign bit is copied in from the left
console.log(-1 >>> 0); // 4294967295: zeros fill from the left, so the result is unsignedCommon Errors
The 32-bit conversion quietly drops information:
console.log(3.7 | 0); // 3
console.log(-3.7 | 0); // -3 (truncates toward zero)
console.log(2 ** 31 | 0); // -2147483648: overflowed into the sign bit
console.log((2 ** 32 + 5) | 0); // 5: the high bits are discarded
console.log('12' & 15); // 12: strings go through ToNumber firstOperators and BigInt
BigInt holds integers of any size. Arithmetic operators accept BigInt, but both operands must be BigInt. JavaScript will not convert between BigInt and Number for you, because either direction could lose precision.
console.log(1n + 2n); // 3n
console.log(7n / 2n); // 3n: BigInt division truncates
console.log(1n == 1); // true
console.log(1n === 1); // false: different types
console.log(2n > 1); // true
try {
1n + 1;
} catch (e) {
console.log(e instanceof TypeError); // true: BigInt and Number cannot be mixed in arithmetic
}Comparisons and == can mix the two types because they compare mathematical values without producing a new number. Strict equality still checks the type first.
Bitwise operators also work on BigInt, with no 32-bit limit, so (2n ** 40n) | 1n keeps all its bits. There are two exceptions. >>> throws a TypeError for BigInt because a BigInt has no fixed width to be unsigned against. Unary +1n also throws, since unary + always means ToNumber. To mix the types, convert one side explicitly with BigInt(x) or Number(x).
Exercise
What do (2 ** 32 + 5) | 0 and 10n + BigInt(5) evaluate to, and what happens with 10n + 5?
Show answer
(2 ** 32 + 5) | 0 is 5 because ToInt32 discards every bit above the low 32. 10n + BigInt(5) is 15n. 10n + 5 throws a TypeError because BigInt and Number cannot be mixed in arithmetic.
Exercise
Why does -1 >>> 0 give 4294967295, but -1n >>> 0n throws?
Show answer
For Numbers, >>> reads the 32-bit pattern of -1 (all ones) as unsigned, which is 2^32 - 1. A BigInt has no fixed bit width, so an unsigned shift has no meaning for it, and JavaScript throws a TypeError.