15 - JavaScript - Bitwise Operators

15 - JavaScript - Bitwise Operators

JavaScript bitwise operators are used to perform bitwise operations on binary numbers. These operators allow you to manipulate individual bits within a binary number, which is useful when working with low-level data such as graphics and cryptography.

Bitwise AND (&)

This operator performs a bitwise AND operation on two numbers, which means it compares each bit of the two numbers and returns 1 for each bit that is 1 in both numbers.

Syntax:

result = num1 & num2;

Example code:

let num1 = 5;    // binary 101
let num2 = 3;    // binary 011
let result = num1 & num2;

console.log(result);   // Output: 1 (binary 001)

Explanation: The bitwise AND operator compares the binary representation of num1 (101) and num2 (011) and returns the binary value of 001, which is 1 in decimal.

Bitwise OR (|)

This operator performs a bitwise OR operation on two numbers, which means it compares each bit of the two numbers and returns 1 for each bit that is 1 in at least one of the two numbers.

Syntax:

result = num1 | num2;

Example code:

let num1 = 5;    // binary 101
let num2 = 3;    // binary 011
let result = num1 | num2;

console.log(result);   // Output: 7 (binary 111)

Explanation: The bitwise OR operator compares the binary representation of num1 (101) and num2 (011) and returns the binary value of 111, which is 7 in decimal.

Bitwise XOR (^)

This operator performs a bitwise XOR operation on two numbers, which means it compares each bit of the two numbers and returns 1 for each bit that is 1 in exactly one of the two numbers.

Syntax:

result = num1 ^ num2;

Example code:

let num1 = 5;    // binary 101
let num2 = 3;    // binary 011
let result = num1 ^ num2;

console.log(result);   // Output: 6 (binary 110)

Explanation: The bitwise XOR operator compares the binary representation of num1 (101) and num2 (011) and returns the binary value of 110, which is 6 in decimal.

Bitwise NOT (~)

This operator performs a bitwise NOT operation on a number, which means it flips all the bits of the number. It is a unary operator.

Syntax:

result = ~num;

Example code:

let num = 5;    // binary 101
let result = ~num;

console.log(result);   // Output: -6 (binary 11111111111111111111111111111010)

Explanation: The bitwise NOT operator flips all the bits of the binary representation of num (101), resulting in the binary value of 11111111111111111111111111111010 in two's complement representation, which is -6 in decimal.

Left shift (<<)

This operator shifts the bits of a number to the left by a specified number of positions. This is equivalent to multiplying the number by 2 to the power of the shift amount.

Syntax:

result = num << shift;

Example code:

let num = 5;    // binary 101
let shift = 2;
let result = num << shift;

console.log(result);   // Output: 20 (binary 10100)

Explanation: The left shift operator shifts the bits of the binary representation of num (101) to the left by shift positions, resulting in the binary value of 10100, which is 20 in decimal.

Right shift (>>)

This operator shifts the bits of a number to the right by a specified number of positions. This is equivalent to dividing the number by 2 to the power of the shift amount.

Syntax:

result = num >> shift;

Example code:

let num = 20;    // binary 10100
let shift = 2;
let result = num >> shift;

console.log(result);   // Output: 5 (binary 101)

Explanation: The right shift operator shifts the bits of the binary representation of num (10100) to the right by shift positions, resulting in the binary value of 101, which is 5 in decimal.

Summarizing Up

JavaScript bitwise operators are used to perform operations on binary numbers, allowing developers to manipulate individual bits within a binary number. The six bitwise operators in JavaScript are the Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^), Bitwise NOT (~), Left shift (<<), and Right shift (>>). The Bitwise AND operator compares each bit of two numbers and returns 1 for each bit that is 1 in both numbers. The Bitwise OR operator compares each bit of two numbers and returns 1 for each bit that is 1 in at least one of the two numbers. The Bitwise XOR operator compares each bit of two numbers and returns 1 for each bit that is 1 in exactly one of the two numbers. The Bitwise NOT operator flips all the bits of a number. The Left shift operator shifts the bits of a number to the left by a specified number of positions, while the Right shift operator shifts the bits of a number to the right by a specified number of positions.

Did you find this article valuable?

Support Bosonique ITEdTech by becoming a sponsor. Any amount is appreciated!