HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript Bitwise Operations

In JavaScript, bitwise operations are operations that manipulate the binary representation of numbers at the bit level.
These operations work with integers by converting them into their binary representation, performing the operation bit by bit, and then converting the result back to an integer.

JavaScript provides several bitwise operators:

  • Bitwise AND (&):
  • Bitwise OR (|):
  • Bitwise XOR (^):
  • Bitwise NOT (~):
  • Left Shift (<<):
  • Sign-propagating Right Shift (>>):
  • Zero-fill Right Shift (>>>):

Bitwise AND (&):

Performs a bitwise AND operation on each pair of corresponding bits. Returns 1 if both bits are 1, otherwise returns 0.

Example

let a = 5;  // binary representation: 0101
let b = 3;  // binary representation: 0011

let result = a & b; // Performs bitwise AND operation

console.log(result); // Output: 1 (binary: 0001)                  
 
You can click on above box to edit the code and run again.

Bitwise OR (|):

Performs a bitwise OR operation on each pair of corresponding bits. Returns 1 if at least one of the bits is 1, otherwise returns 0.

Example

let a = 5;  // binary representation: 0101
let b = 3;  // binary representation: 0011

console.log(a | b); // Output: 7 (binary: 0111)                  
You can click on above box to edit the code and run again.

Bitwise XOR (^):

Performs a bitwise XOR (exclusive OR) operation on each pair of corresponding bits. Returns 1 if the bits are different, otherwise returns 0.

Example

 let a = 5;  // binary representation: 0101
let b = 3;  // binary representation: 0011

console.log(a ^ b); // Output: 6 (binary: 0110)                 
You can click on above box to edit the code and run again.

Bitwise NOT (~):

Performs a bitwise NOT (complement) operation on each bit, inverting all bits. Returns the one's complement representation of the number.

Example

 let a = 5;  // binary representation: 0101
let b = 3;  // binary representation: 0011
 
 console.log(~a);    // Output: -6 (binary: 1111 1111 1111 1010)               
              
You can click on above box to edit the code and run again.

Left Shift (<<):

Shifts the bits of the left operand to the left by the number of positions specified by the right operand. Zeros are shifted in from the right.

Example

let a = 5;  // binary representation: 0101
let b = 3;  // binary representation: 0011

 console.log(a << 1); // Output: 10 (binary: 1010)
 
You can click on above box to edit the code and run again.

Sign-propagating Right Shift (>>):

Shifts the bits of the left operand to the right by the number of positions specified by the right operand. The sign bit is preserved, and zeros are shifted in from the left.

Example

 let a = 5;  // binary representation: 0101
let b = 3;  // binary representation: 0011
 
 console.log(b >> 1); // Output: 1 (binary: 0001)              
              
You can click on above box to edit the code and run again.

Zero-fill Right Shift (>>>):

Shifts the bits of the left operand to the right by the number of positions specified by the right operand. Zeros are shifted in from the left, and the sign bit is ignored.

Example

 let a = 5;  // binary representation: 0101
let b = 3;  // binary representation: 0011
 
 console.log(b >>> 1); // Output: 1 (binary: 0001)                
You can click on above box to edit the code and run again.

Output