DEV Community

Theophilus Kolawole
Theophilus Kolawole

Posted on

JavaScript Bitwise Operators

In JavaScript, numbers are stored as 64-bit floating points. Bitwise operators in JavaScript operate on 32-bit operands.

Before performing a bitwise operation, JavaScript converts 64-bit floating point numbers into 32-bit signed integers, it then converts the result back to 64-bit numbers.

Examples of JavaScript Bitwise Operators

1. JavaScript Bitwise AND (&)

When the JavaScript Bitwise AND operation is performed on a pair of bits, it returns 1 if both bits are 1 otherwise it returns 0.

Below is an example of JavaScript Bitwise AND operation
let x = 10;
let y = 6;
let result = x & y;
console.log(result); // 2

Run code on codetutorialz

2. JavaScript Bitwise OR (|)

When the JavaScript Bitwise OR operation is performed on a pair of bits, it returns 1 if one of the bits is 1 otherwise it returns 0

Example of JavaScript Bitwise OR operation
let x = 10;
let y = 6;
let result = x | y;
console.log(result); // 14

Run code on codetutorialz

3. JavaScript Bitwise XOR (^)

When the JavaScript Bitwise XOR operation is performed on a pair of bits, it returns 1 if the corresponding bits are different and returns 0 if they are the same.

Example of JavaScript Bitwise XOR operation
let x = 10;
let y = 6;
let result = x ^ y;
console.log(result); // 12

Run code on codetutorialz

4. Bitwise NOT (~)

The JavaScript Bitwise NOT operator flips the bit (if the bit is 1 it becomes 0, and if the bit is 0, it becomes 1).

The example below performs a JavaScript Bitwise NOT operation on 6
let x = 6;
let result = ~x;
console.log(result); // -7

Run code on codetutorialz

5. Bitwise Zero Fill Left Shift <<

The bitwise zero fill left shift operation push in one or more zero bits from the right and the leftmost bits fall off. The operand before the << operator specifies the number to be shifted, and the operand after the << operator specifies the number of times to shift (number of 0 to be added).

The example below performs a JavaScript Bitwise Left Shift operation on 10 and 2
let result = 10 << 2;
console.log(result); // 40

Run code on codetutorialz

6. Bitwise Sign Preserving Right Shift >>

The bitwise sign preserving right shift operation push in copies of the leftmost bits from the left and the rightmost bits fall of. The operand before the >> operator, specifies the number to be shifted, and the operand after the >> operator specifies the number of times to shift.

The example below performs a JavaScript Bitwise Sign Preserving Right Shift operation on -10 and 2
let result = -10 >> 2;
console.log(result); // -3

Run code on codetutorialz

7. Bitwise Zero Fill Right Shift >>>

The bitwise zero-fill right shift operation push in one or more zero bits from the left and the rightmost bits fall off. The operand before the >>> operator specifies the number to be shifted, and the operand after the >>> operator specifies the number of times to shift (number of 0 to be added).

The example below performs a JavaScript Bitwise Zero Fill Right Shift operation on 10 and 2
let result = 10 >>> 2;
console.log(result); // 2

Run code on codetutorialz

Learn more about JavaScript Bitwise Operator here
Complete JavaScript Tutorial

Top comments (0)