JavaScript Bitwise Operators

In JavaScript, Bitwise Operators are used to perform operations on the binary representations of integers. These operators treat their operands as 32-bit signed integers and work on each individual bit.

Here’s an overview of the bitwise operators in JavaScript:

1. AND (&)

The AND operator compares each bit of two numbers and returns 1 if both bits are 1, otherwise, it returns 0.

Example:

let a = 5;   // 0101 in binary
let b = 3;   // 0011 in binary
let result = a & b;  // 0001 in binary, which is 1
console.log(result); // 1

Try It Now

2. OR (|)

The OR operator compares each bit of two numbers and returns 1 if at least one of the bits is 1, otherwise, it returns 0.

Example:

let a = 5;   // 0101 in binary
let b = 3;   // 0011 in binary
let result = a | b;  // 0111 in binary, which is 7
console.log(result); // 7

Try It Now

3. XOR (^)

The XOR (exclusive OR) operator compares each bit of two numbers and returns 1 if the bits are different, otherwise, it returns 0.

Example:

let a = 5;   // 0101 in binary
let b = 3;   // 0011 in binary
let result = a ^ b;  // 0110 in binary, which is 6
console.log(result); // 6

Try It Now

4. NOT (~)

The NOT operator inverts all the bits of its operand. It changes 1 to 0 and 0 to 1. It also performs a two’s complement conversion on the number.

Example:

let a = 5;   // 0101 in binary
let result = ~a;  // Inverts all bits: 1010 (which is -6 in decimal, two's complement)
console.log(result); // -6

Try It Now

5. Left Shift (<<)

The Left Shift operator shifts the bits of a number to the left by the specified number of positions. Zeros are added on the right side.

Example:

let a = 5;   // 0101 in binary
let result = a << 1;  // Shift left by 1 bit: 1010 in binary, which is 10
console.log(result); // 10

Try It Now

6. Right Shift (>>)

The Right Shift operator shifts the bits of a number to the right by the specified number of positions. For positive numbers, zeros are added on the left side, and for negative numbers, ones are added to the left (sign extension).

Example:

let a = 5;   // 0101 in binary
let result = a >> 1;  // Shift right by 1 bit: 0010 in binary, which is 2
console.log(result); // 2

Try It Now

7. Zero-fill Right Shift (>>>)

The Zero-fill Right Shift operator shifts the bits of a number to the right by the specified number of positions, but it always fills zeros on the left, regardless of the number’s sign.

Example:

let a = -5;  // 11111111111111111111111111111011 in binary (two's complement representation)
let result = a >>> 1;  // Shift right by 1 bit and fill with zero
console.log(result); // 2147483642

Try It Now

Bitwise Operators Summary:

  • & (AND): Returns 1 only if both bits are 1.
  • | (OR): Returns 1 if at least one of the bits is 1.
  • ^ (XOR): Returns 1 if the bits are different.
  • ~ (NOT): Inverts the bits.
  • << (Left Shift): Shifts bits to the left, filling with zeros.
  • >> (Right Shift): Shifts bits to the right, preserving the sign.
  • >>> (Zero-fill Right Shift): Shifts bits to the right, always filling with zeros.

Example Combining Operators:

let a = 5;  // 0101 in binary
let b = 3;  // 0011 in binary

let andResult = a & b;  // 0001 in binary, 1
let orResult = a | b;   // 0111 in binary, 7
let xorResult = a ^ b;  // 0110 in binary, 6
let notResult = ~a;     // 1010 in binary, -6

console.log(andResult, orResult, xorResult, notResult); // 1 7 6 -6

Try It Now

Use Cases for Bitwise Operators:

  • Efficient numerical operations: Often used in performance-critical applications or embedded systems where working directly with binary data is essential.
  • Manipulating flags: Bitwise operators are commonly used in scenarios involving flags or permission settings, such as enabling/disabling features.
  • Masking and extracting bits: You can use bitwise operators to “mask” or extract specific bits from a number.

Although bitwise operators may seem low-level, they are very powerful tools for handling binary data and performing efficient operations on numbers in JavaScript.