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 bitwise operators in JavaScript

OperatorNameDescription
&ANDreturns 1 if both bits are 1
OR
^XORreturns 1 if only one of the two bits is 1
~NOTInverts all the bits (1 becomes 0, and 0 becomes 1)
<<Zero-fill left shiftShifts left by pushing zeros in from the right and letting the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>>Zero-fill right shiftShifts right by pushing zeros in from the left, and let the rightmost bits fall off

Note: The maximum integer that is representable through a 32-bit signed number is 2147483647 and the minimum is -2147483648

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.

Operand 1Operand 2Bitwise AND operation
000
010
100
111

The example below shows the working of the bitwise AND operation between two integers 10 and 6

// From decimal to binary
10 = 00001010
6  = 00000110

// Bitwise AND operation of 10 and 6
    00001010
&   00000110
    --------
    00000010 = 2 (In decimal)

Example of JavaScript Bitwise AND operation

let x = 10;
let y = 6;
let result = x & y;
console.log(result); // 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

Operand 1Operand 2Bitwise OR Operation
000
011
101
111

The example below shows the working of the bitwise OR operation between two integers 10 and 6

// From decimal to binary
10 = 00001010
6  = 00000110

// Bitwise OR operation of 10 and 6
    00001010
|   00000110
    --------
    00001110 = 14 (In decimal)

Example of JavaScript Bitwise OR operation

let x = 10;
let y = 6;
let result = x | y;
console.log(result); // 14

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.

Operand 1Operand 2Bitwise XOR
000
011
101
110

The example below shows the working of the bitwise XOR operation between two integers 10 and 6

// From decimal to binary
10 = 00001010
6  = 00000110

// Bitwise XOR operation of 10 and 6
    00001010
^   00000110
    --------
    00001100 = 12 (In decimal)

Example of JavaScript Bitwise XOR operation

let x = 10;
let y = 6;
let result = x ^ y;
console.log(result); // 12

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 shows the working of bitwise NOT operation on 6

// From decimal to binary
6  = 00000000000000000000000000000110

// Bitwise NOT operation of 6
~   00000000000000000000000000000110
    --------------------------------
    11111111111111111111111111111001 = -7 (In decimal)

The example below performs a JavaScript Bitwise NOT operation on 6

let x = 6;
let result = ~x;
console.log(result); // -7

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 shows the working of bitwise zero fill left shift operation of 10 << 2

// From decimal to binary
10 = 00000000000000000000000000001010

// Bitwise Left Shift operation of 10 << 2
<<  00000000000000000000000000101000 = 40 (In decimal)

The example below performs a JavaScript Bitwise Left Shift operation on 10 and 2

let result = 10 << 2;
console.log(result); // 40

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 off. The operand before the >> operator, specify the number to be shifted, and the operand after the >> operator specifies the number of times to shift.

The example below shows the working of the bitwise Sign Preserving Right Shift operation of -10 >> 2

// From decimal to binary
-10 = 11111111111111111111111111110110

// Bitwise Sign Preserving Right Shift operation of -10 << 2
<<  11111111111111111111111111111101 = -3 (In decimal)

The example below performs a JavaScript Bitwise Sign Preserving Right Shift operation on -10 and 2

let result = -10 >> 2;
console.log(result); // -3

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 shows the working of the bitwise Zero Fill Right Shift operation of 10 >>> 2

// From decimal to binary
10 = 00000000000000000000000000001010

// Bitwise Left Shift operation of 10 >>> 2
<<<  00000000000000000000000000000010 = 2 (In decimal)

The example below performs a JavaScript Bitwise Zero Fill Right Shift operation on 10 and 2

let result = 10 >>> 2;
console.log(result); // 2

Learn more about JavaScript