JavaScript Bitwise Operators

JavaScript Bitwise Operators

Operator Name Description
& AND Set each bit to 1 if both bits are 1
| OR Set each bit to 1 if one of the two bits is 1
^ XOR Set each bit to 1 if only one of the two bits is 1
~ NOT Reverse all bits
<< Zero-filled left shift By pushing a zero from the right to the left, and dropping the leftmost bit.
>> Signed right shift By pushing a copy of the leftmost bit from the left to the right, and dropping the rightmost bit.
>>> Zero-filled right shift By pushing a zero from the left to the right, and dropping the rightmost bit.

Example

Operation Result is equivalent to Result
5 & 1 1 0101 & 0001 0001
5 | 1 5 0101 | 0001 0101
5 ^ 1 4 0101 ^ 0001 0100
~ 5 10 ~0101 1010
5 << 1 10 0101 << 1 1010
5 >> 1 2 0101 >> 1 0010
5 >>> 1 2 0101 >>> 1 0010

JavaScript uses 32-bit bitwise operands

JavaScript stores numbers as 64-bit floating-point numbers, but all bitwise operations are performed with 32-bit binary numbers.

Before performing bitwise operations, JavaScript converts numbers to 32-bit signed integers.

After performing the bitwise operation, the result will be converted back to a 64-bit JavaScript number.

The above example uses 4-bit unsigned binary numbers. Therefore, ~5 returns 10.

Since JavaScript uses 32-bit signed integers, JavaScript will return -6.

00000000000000000000000000000101 (5)

11111111111111111111111111111010 (~5 = -6)

Signed integers use the leftmost bit as the minus sign.

Bitwise AND

When performing bitwise AND on a pair of bits, if both bits are 1, it returns 1.

Unit example:

Operation Result
0 & 0 0
0 & 1 0
1 & 0 0
1 & 1 1

Four-bit example:

Operation Result
1111 & 0000 0000
1111 & 0001 0001
1111 & 0010 0010
1111 & 0100 0100

Bitwise OR

When performing bitwise OR on a pair of bits, if one of the bits is 1, it returns 1:

Unit example

Operation Result
0 | 0 0
0 | 1 1
1 | 0 1
1 | 1 1

Four-bit operation:

Operation Result
1111 | 0000 1111
1111 | 0001 1111
1111 | 0010 1111
1111 | 0100 1111

Bitwise XOR

When performing bitwise XOR on a pair of bits, if the bits are different, it returns 1:

Unit example:

Operation Result
0 ^ 0 0
0 ^ 1 1
1 ^ 0 1
1 ^ 1 0

Four-bit example:

Operation Result
1111 ^ 0000 1111
1111 ^ 0001 1110
1111 ^ 0010 1101
1111 ^ 0100 1011

JavaScript Bitwise AND (&)

If both bits are 1, the bitwise AND operation returns 1:

Decimal Binary
5 00000000000000000000000000000101
1 00000000000000000000000000000001
5 & 1 00000000000000000000000000000001 (1)

Example

var x = 5 & 1;

Try It Yourself

JavaScript Bitwise OR (|)

If one of the bits is 1, the bitwise OR operation returns 1:

Decimal Binary
5 00000000000000000000000000000101
1 00000000000000000000000000000001
5 | 1 00000000000000000000000000000101 (5)

Example

var x = 5 | 1;

Try It Yourself

JavaScript bitwise XOR (^)

If the bits are different, XOR returns 1:

Decimal Binary
5 00000000000000000000000000000101
1 00000000000000000000000000000001
5 ^ 1 00000000000000000000000000000100 (4)

Example

var x = 5 ^ 1;

Try It Yourself

JavaScript bitwise NOT (~)

Decimal Binary
5 00000000000000000000000000000101
~5 11111111111111111111111111111010 (-6)

Example

var x = ~5;

Try It Yourself

JavaScript (zero-filled) bitwise left shift (<<)

This is zero-filled left shift. One or more zero bits are pushed from the right, and the leftmost bit is removed:

Decimal Binary
5 00000000000000000000000000000101
5 << 1 00000000000000000000000000001010 (10)

Example

var x = 5 << 1;

Try It Yourself

JavaScript (signed) bitwise right shift (>>)

This is sign-maintaining right shift. The leftmost bit is pushed from the left, and the rightmost bit is shifted out:

Decimal Binary
-5 11111111111111111111111111111011
-5 >> 1 11111111111111111111111111111101 (-3)

Example

var x = -5 >> 1;

Try It Yourself

JavaScript (zero-filled) right shift (>>>)

This is zero-filled right shift. One or more zero bits are pushed from the left, and the rightmost bit is shifted out:

Decimal Binary
5 00000000000000000000000000000101
5 >>> 1 00000000000000000000000000000010 (2)

Example

var x = 5 >>> 1;

Try It Yourself

32-bit signed integer (binary number)

It is very easy to understand the 32-bit integer with only one set bit:

Binary Representation Decimal Value
00000000000000000000000000000001 1
00000000000000000000000000000010 2
00000000000000000000000000000100 4
00000000000000000000000000001000 8
00000000000000000000000000010000 16
00000000000000000000000000100000 32
00000000000000000000000001000000 64

Adding more digits reveals the pattern of binary:

Binary Representation Decimal Value
00000000000000000000000000000101 5 (4 + 1)
00000000000000000000000000101000 40 (32 + 8)
00000000000000000000000000101101 45 (32 + 8 + 4 + 1)

Negative numbers are the binary complement of positive numbers plus 1:

Binary Representation Decimal Value
00000000000000000000000000000101 5
11111111111111111111111111111011 -5
00000000000000000000000000101000 40
11111111111111111111111111011000 -40
11111111111111111111111111011001 -41

Convert Decimal to Binary

Example

function dec2bin(dec){
    return (dec >>> 0).toString(2);
}

Try It Yourself

Convert Binary to Decimal

Example

function bin2dec(bin){
    return parseInt(bin, 2).toString(10);
}

Try It Yourself