JavaScript bitwise operators are used to perform bitwise operations on integers. These operations treat their operands as a sequence of 32 bits (zeros and ones) rather than as decimal, hexadecimal, or octal numbers. This can be particularly useful for certain low-level operations, such as encoding and decoding information, manipulating bits in a bit field, or performing certain arithmetic operations more efficiently.


Types of Bitwise Operators

JavaScript supports the following bitwise operators:


Bitwise AND (&):

The bitwise AND operator (&) compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.


Example:

 
let result = 5 & 3; // 0101 & 0011 = 0001 (1 in decimal)
console.log(result); // Output: 1
Bitwise OR (|):


The bitwise OR operator (|) compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. If both bits are 0, the result bit is set to 0.
 

Example:
 
let result = 5 | 3; // 0101 | 0011 = 0111 (7 in decimal)
console.log(result); // Output: 7
Bitwise XOR (^):
 

The bitwise XOR operator (^) compares each bit of its first operand to the corresponding bit of its second operand. If the bits are different, the corresponding result bit is set to 1. If the bits are the same, the result bit is set to 0.


Example:
 
let result = 5 ^ 3; // 0101 ^ 0011 = 0110 (6 in decimal)
console.log(result); // Output: 6
Bitwise NOT (~):


The bitwise NOT operator (~) inverts all the bits of its operand. This can be useful for reversing the bits in a value.
 

Example:
 
let num = 5; // 00000000000000000000000000000101 (32-bit representation)
let result = ~num; // ~00000000000000000000000000000101 = 11111111111111111111111111111010 (-6 in decimal)
console.log(result); // Output: -6


Left Shift (<<):

The left shift operator (<<) moves the bits of its first operand to the left by the number of positions specified by the second operand. Excess bits shifted off to the left are discarded, and empty positions are set to 0.


Example:
 
let result = 5 << 1; // 0101 << 1 = 1010 (10 in decimal)
console.log(result); // Output: 10
Sign-propagating Right Shift (>>):

The sign-propagating right shift operator (>>) moves the bits of its first operand to the right by the number of positions specified by the second operand. Bits that are shifted off to the right are discarded. The leftmost bits are filled with the sign bit (0 for positive


numbers, 1 for negative numbers).

Example:

 
let result = 5 >> 1; // 0101 >> 1 = 0010 (2 in decimal)
console.log(result); // Output: 2


Zero-fill Right Shift (>>>):

The zero-fill right shift operator (>>>) moves the bits of its first operand to the right by the number of positions specified by the second operand. Bits that are shifted off to the right are discarded. The leftmost bits are filled with 0.


Example:
 
let result = 5 >>> 1; // 0101 >>> 1 = 0010 (2 in decimal)
console.log(result); // Output: 2


Practical Examples

Example 1: Checking Bitwise AND Operation


let a = 5;    // 0101 in binary
let b = 3;    // 0011 in binary
let result = a & b;   // 0101 & 0011 = 0001 (1 in decimal)
console.log(result);  // Output: 1
 

Example 2: Performing Bitwise XOR Operation
 

let a = 5;    // 0101 in binary
let b = 3;    // 0011 in binary
let result = a ^ b;   // 0101 ^ 0011 = 0110 (6 in decimal)
console.log(result);  // Output: 6


Example 3: Using Bitwise Left Shift Operator
 

let a = 5;    // 0101 in binary

let result = a << 1;   // 0101 << 1 = 1010 (10 in decimal)

console.log(result);  // Output: 10


Use Cases
  • Data Compression: Bitwise operators can be used to compress data by manipulating bits directly.
  • Image Processing: Operations such as cropping, flipping, or rotating images can be done using bitwise operators.
  • Networking: Bitwise operators can be used to implement network protocols and data transmission.
  • Cryptography: Bitwise operations are fundamental in encryption and decryption algorithms.



Practice Excercise Practice now