JavaScript Bitwise Operations


JavaScript Bitwise Operators

Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shifts left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>> Zero fill right shift Shifts right by pushing zeros in from the left, and let the rightmost bits fall off

Examples

Operation Result Same as Result
5 & 1 1 0101 & 0001  0001
5 | 1 5 0101 | 0001  0101
~ 5 10  ~0101  1010
5 << 1 10 0101 << 1  1010
5 ^ 1 4 0101 ^ 0001  0100
5 >> 1 2 0101 >> 1  0010
5 >>> 1 2 0101 >>> 1  0010

JavaScript Uses 32 bits Bitwise Operands

JavaScript saves numbers as 64-bit floating-point numbers, but it does bitwise operations using 32-bit binary numbers.

Prior to conducting a bitwise operation, JavaScript changes numbers into 32-bit signed integers.

After doing the bitwise operation, the outcome is changed back to 64-bit JavaScript numbers.

The examples above show how to use 4-bit unsigned binary numbers. Because of this ~ 5 returns 10.

JavaScript doesn't give 10 because it uses 32-bit numbers. It will return -6.

00000000000000000000000000000101 (5)

11111111111111111111111111111010 (~5 = -6)

A negative number is indicated by the leftmost bit being 1.


JavaScript Bitwise AND

When you do a bitwise AND on two bits, you get 1 only if both bits are 1.

One bit example:
OperationResult
0 & 00
0 & 10
1 & 00
1 & 11
4 bits example:
OperationResult
1111 & 00000000
1111 & 00010001
1111 & 00100010
1111 & 01000100

JavaScript Bitwise OR

When you perform a bitwise OR on two bits, the result is 1 if at least one of the bits is 1.

One bit example:
OperationResult
0 | 00
0 | 1
1 | 01
1 | 11
4 bits example:
OperationResult
1111 | 00001111
1111 | 00011111
1111 | 00101111
1111 | 01001111

JavaScript Bitwise XOR

When you do a bitwise XOR on two bits, it gives you 1 if the bits are not the same.

One bit example:
OperationResult
0 ^ 00
0 ^ 1
1 ^ 01
1 ^ 1
4 bits example:
OperationResult
1111 ^ 00001111
1111 ^ 00011110
1111 ^ 00101101
1111 ^ 01001011

JavaScript Bitwise AND (&)

Bitwise AND returns 1 only if both bits are 1:

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 & 100000000000000000000000000000001 (1)

JavaScript Bitwise OR (|)

The Bitwise OR operation gives a result of 1 when at least one of the bits being compared is 1.

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 | 100000000000000000000000000000101 (5)

JavaScript Bitwise XOR (^)

XOR operation in bitwise returns 1 when comparing different bits.

DecimalBinary
500000000000000000000000000000101
100000000000000000000000000000001
5 ^ 100000000000000000000000000000100 (4)

JavaScript Bitwise NOT (~)

DecimalBinary
500000000000000000000000000000101
~511111111111111111111111111111010 (-6)

JavaScript (Zero Fill) Bitwise Left Shift (<<)

This operation is called a zero fill left shift. It involves adding one or more zero bits to the left side, and the bits on the far left are dropped off.

DecimalBinary
500000000000000000000000000000101
5 << 100000000000000000000000000001010 (10)

JavaScript (Sign Preserving) Bitwise Right Shift (>>)

This is a type of right shift that maintains the sign. It brings in copies of the leftmost bit from the left, and the rightmost bits drop off.

DecimalBinary
-511111111111111111111111111111011
-5 >> 111111111111111111111111111111101 (-3)

JavaScript (Zero Fill) Right Shift (>>>)

This operation is called a zero-fill right shift. It means that one or more zero bits are added from the left side, and the bits on the right side are dropped off.

DecimalBinary
500000000000000000000000000000101
5 >>> 100000000000000000000000000000010 (2)

Binary Numbers

Binary numbers with only one bit set are easy to understand:

Binary RepresentationDecimal value
000000000000000000000000000000011
000000000000000000000000000000102
000000000000000000000000000001004
000000000000000000000000000010008
0000000000000000000000000001000016
0000000000000000000000000010000032
0000000000000000000000000100000064

Increasing the number of bits exposes the binary pattern.

Binary RepresentationDecimal value
000000000000000000000000000001015 (4 + 1)
0000000000000000000000000000110113 (8 + 4 + 1)
0000000000000000000000000010110145 (32 + 8 + 4 + 1)

JavaScript saves binary numbers using a two's complement format.

This implies that the bitwise NOT of a number is equal to the negative of that number plus 1.

Binary RepresentationDecimal value
000000000000000000000000000001015
11111111111111111111111111111011-5
000000000000000000000000000001106
11111111111111111111111111111010-6
0000000000000000000000000010100040
11111111111111111111111111011000-40

Joke:

In the world, there are just two kinds of people: those who get binary code and those who don't.


Converting Decimal to Binary


Converting Binary to Decimal