JavaScript : Bitwise Operations

In JavaScript, bitwise operations are operations that manipulate individual bits of binary representations of numbers. These operations treat their operands as a sequence of 32 bits (signed 32-bit integers) and perform bitwise operations on each corresponding pair of bits. Here's an overview of common bitwise operators in JavaScript:

1. Bitwise AND ( & )


The bitwise AND operator performs a bitwise AND operation between each pair of corresponding bits of its operands. It returns a 1 in each bit position where both operands have a 1.


const result = 5 & 3;
// Binary representation: 101 & 011
// Result: 001 (1)
console.log(result); // Output: 1

 

2. Bitwise OR ( | )


The bitwise OR operator performs a bitwise OR operation between each pair of corresponding bits of its operands. It returns a 1 in each bit position where at least one of the operands has a 1.


const result = 5 | 3;
// Binary representation: 101 | 011
// Result: 111 (7)
console.log(result); // Output: 7

 

3. Bitwise XOR ( ^ )


The bitwise XOR operator performs a bitwise exclusive OR operation between each pair of corresponding bits of its operands. It returns a 1 in each bit position where only one of the operands has a 1.


const result = 5 ^ 3;
// Binary representation: 101 ^ 011
// Result: 110 (6)
console.log(result); // Output: 6

 

4. Bitwise NOT ( ~ )


The bitwise NOT operator inverts the bits of its single operand. It returns the one's complement of the binary representation of the operand.


const result = ~5;
// Binary representation of 5: 00000000000000000000000000000101
// Result: 11111111111111111111111111111010 (-6 in two's complement form)
console.log(result); // Output: -6

 

5. Left Shift ( << )


The left shift operator shifts the bits of its first operand to the left by the number of positions specified by the second operand. The vacant bits are filled with zeros.


const result = 5 << 1;
// Binary representation of 5: 00000000000000000000000000000101
// Result: 00000000000000000000000000001010 (10)
console.log(result); // Output: 10

 

6. Signed Right Shift ( >> )


The signed right shift operator shifts the bits of its first operand to the right by the number of positions specified by the second operand. The vacant bits are filled with the sign bit (the leftmost bit).


const result = 5 >> 1;
// Binary representation of 5: 00000000000000000000000000000101
// Result: 00000000000000000000000000000010 (2)
console.log(result); // Output: 2

 

7. Unsigned Right Shift ( >>> )


The unsigned right shift operator shifts the bits of its first operand to the right by the number of positions specified by the second operand. The vacant bits are filled with zeros.


const result = -5 >>> 1;
// Binary representation of -5: 11111111111111111111111111111011
// Result: 01111111111111111111111111111101 (2147483645)
console.log(result); // Output: 2147483645

 

Bitwise operations are typically used in low-level programming, such as optimizing algorithms, cryptography, and device driver development. They can also be used in JavaScript for performance optimization in certain scenarios, though they are less common in high-level JavaScript development.