Bitwise operators
suggest changeExcept for operations on BigInt, JavaScript bitwise operators coerce operands to 32-bit integers. Signed operators use two's-complement values; >>> produces an unsigned 32-bit result. For those 32-bit operations, shift counts use only their low five bits, so shifting by 32 is equivalent to shifting by 0. BigInt shifts are not masked to five bits.
Bitwise or
// eslint-disable-next-line prefer-const -- Reassigned in later examples.
let a = 0b0011 | 0b1010; // a === 0b1011
// truth table
// 1010 | (or)
// 0011
// 1011 (result)
Bitwise and
a = 0b0011 & 0b1010; // a === 0b0010
// truth table
// 1010 & (and)
// 0011
// 0010 (result)
Bitwise not
a = ~0b0011; // a === -4
// 00000000000000000000000000000011
// 11111111111111111111111111111100 (two's-complement -4)
Bitwise xor (exclusive or)
a = 0b1010 ^ 0b0011; // a === 0b1001
// truth table
// 1010 ^ (xor)
// 0011
// 1001 (result)
Bitwise left shift
a = 0b0001 << 1; // a === 0b0010
a = 0b0001 << 2; // a === 0b0100
a = 0b0001 << 3; // a === 0b1000
After 32-bit coercion, a left shift by n is equivalent to multiplication by 2 ** n with overflow confined to 32 bits. Use multiplication for ordinary arithmetic; bit shifts communicate bit manipulation and should not be treated as a general performance optimization.
const n = 2;
let a = 5.4;
const result = (a << n) === Math.trunc(a) * (2 ** n);
// result is true
a = 5.4 << n; // 20
Bitwise right shift >> (Sign-propagating shift) >>> (Zero-fill right shift)
a = 0b1001 >> 1; // a === 0b0100
a = 0b1001 >> 2; // a === 0b0010
a = 0b1001 >> 3; // a === 0b0001
a = 0b1001 >>> 1; // a === 0b0100
a = 0b1001 >>> 2; // a === 0b0010
a = 0b1001 >>> 3; // a === 0b0001
A negative 32-bit integer has its most significant bit set:
a = 0b11111111111111111111111111110111 | 0;
console.log(a); // -9
let b = a >> 2; // Copies the sign bit into the vacated bits.
console.log(b); // -3
b = a >>> 2; // Fills the vacated bits with zeroes.
console.log(b); // 2147483643
The result of a >>> operation is always non-negative.
The >> operator preserves the sign bit.
For non-negative 32-bit integers, shifting right by n is equivalent to dividing by 2 ** n and taking the integer part:
let a = 256.67;
let n = 4;
let result = (a >> n) === Math.floor(Math.trunc(a) / (2 ** n));
// result is true
a = a >> n; // 16
result = (a >>> n) === Math.floor(Math.trunc(a) / (2 ** n));
// result is true
a = a >>> n; // 16
For a negative operand, >>> first interprets the 32-bit pattern as unsigned, so it is not equivalent to ordinary signed division:
let a = -256.67;
let n = 4;
let result = (a >>> n) === Math.floor(Math.trunc(a) / (2 ** n));
// result is false
Bitwise assignment operators
With the exception of not (~) all the above bitwise operators can be used as assignment operators:
a |= b; // same as: a = a | b;
a ^= b; // same as: a = a ^ b;
a &= b; // same as: a = a & b;
a >>= b; // same as: a = a >> b;
a >>>= b; // same as: a = a >>> b;
a <<= b; // same as: a = a << b;
Bitwise operators act on numeric values and have no observable byte order. Endianness matters only when interpreting the bytes of an ArrayBuffer. Use DataView and pass the desired byte order explicitly when reading or writing portable binary formats.
Bitwise operators such as & and | are not the same as the logical operators && and ||. The ^ operator means bitwise XOR; exponentiation uses **.