JavaScript Bitwise

Bitwise operations in JavaScript manipulate individual bits within binary representations of numbers.

These operations can be useful for certain low-level programming tasks, such as optimizing algorithms or working with binary data.

Let's delve into the world of bitwise operations with code examples:

Bitwise AND (&)

The bitwise AND operation (&)sets each bit to 1 if both corresponding bits are 1.


const result = 5 & 3; // Bitwise AND of 0101 & 0011
console.log(result); // Output: 1 (0001 in binary)

In the above example, 5 & 3results in 0001in binary, which equals 1in decimal.

Example


const result = 5 & 3; // Bitwise AND of 0101 & 0011
console.log(result); // Output: 1 (0001 in binary)

Bitwise OR (|)

The bitwise OR operation (|)sets each bit to 1 if either corresponding bit is 1.


const result = 5 | 3; // Bitwise OR of 0101 | 0011
console.log(result); // Output: 7 (0111 in binary)

In the above example, the 5 | 3results in 0111in binary, which equals 7in decimal.

Example


const result = 5 | 3; // Bitwise OR of 0101 | 0011
console.log(result); // Output: 7 (0111 in binary)

Bitwise XOR (^)

The bitwise XOR operation (^)sets each bit to 1 if exactly one corresponding bit is 1.


const result = 5 ^ 3; // Bitwise XOR of 0101 ^ 0011
console.log(result); // Output: 6 (0110 in binary)

In the above example, the 5 ^ 3results in 0110in binary, which equals 6in decimal.

Example


const result = 5 ^ 3; // Bitwise XOR of 0101 ^ 0011
console.log(result); // Output: 6 (0110 in binary)

Bitwise NOT (~)

The bitwise NOT operation (~)inverts all bits of its operand.


const result = ~5; // Bitwise NOT of 0101
console.log(result); // Output: -6 (1010 in two's complement binary)

In the above example, the ~5results in 1010in binary, which equals -6in two's complement binary representation.

Example


const result = ~5; // Bitwise NOT of 0101
console.log(result); // Output: -6 (1010 in two's complement binary)

Bitwise Left Shift (<<)

The bitwise left shift operation (<<)shifts the bits of its operand to the left by a specified number of positions.


const result = 5 << 2; // Left shift of 0101 by 2 positions
console.log(result); // Output: 20 (10100 in binary)

In the above example, the 5 << 2shifts the bits of 0101two positions to the left, resulting in 01100in binary, which equals 20in decimal.

Example


const result = 5 << 2; // Left shift of 0101 by 2 positions
console.log(result); // Output: 20 (10100 in binary)

Bitwise Right Shift (>>)

The bitwise right shift operation (>>)shifts the bits of its operand to the right by a specified number of positions.


const result = 20 >> 2; // Right shift of 10100 by 2 positions
console.log(result); // Output: 5 (0101 in binary)

In the above example, the 20 >> 2shifts the bits of 10100two positions to the right, resulting in 0101in binary, which equals 5in decimal.

Example


const result = 20 >> 2; // Right shift of 10100 by 2 positions
console.log(result); // Output: 5 (0101 in binary)
                

Bitwise operations in JavaScript can be powerful tools for performing low-level manipulations on binary data

However, they should be used judiciously, as they can be challenging to understand and may lead to unexpected results if misused.