JavaScript Bitwise Operations

By | September 30, 2021

JavaScript support 7 bitwise operators that perform bits operation between two binary values using 32 bits.

Although JavaScript store all the numbers as 64 bits floating points, but when its comes to bitwise operation it treats the 64 bits numbers as 32 bits binary numbers. This means before the bitwise operation performs on the numbers the JavaScript engine converts the 64 bits number to 32 bits binary number then performs the operation. And once the operation is performed JS converts back the result of the operation to 64 bits umber.

JavaScript Bitwise Operators

 Bitwise Operator Name Description & AND Perform And operation between bits. Set bit to 1 if both the bits are 1. | OR Perform OR operation between bits. Set the bit to 1 if either of the bits is 1. ^ XOR Perform XOR operation between bits. Set the bit to 1 if only one of the bits is 1. ~ NOT Invert the bit from 0 to 1 and vice versa << Zero fill left shift Falloff the left most bit, by pushing zero from the right. >> Signed right shift Falloff the rightmost bit, by pushing the copies of the list most bit from the left. >>> Zero fill right shift Falloff the rightmost bit, by pushing zero from the left.

1. Bitwise AND operator (&)

The AND bitwise operator is represented using & symbol. It can be operated between two JavaScript numbers. It first converts the number into 32-bit binary numbers, performs AND operation between them, and returns back the result in a 64-bit Decimal number.

AND operation table

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

Example

5 & 4;   // 4

Behind the code

5 =(32 bit binary )=  00000000 00000000 00000000 00000101
4 = (32 bit binary)=  00000000 00000000 00000000 00000100
& = (AND operation)=  00000000 00000000 00000000 00000100
00000000 00000000 00000000 00000100  =(64 Bit Number) = 4
That's why 5 & 4  result to 4

2. Bitwise OR Operator (|)

The bitwise OR operator also perform the bit OR operation between two number by converting them into 32 bits binary number. The OR operator returns 1 if any of the value of the bits is 1, else it returns 0.

OR table

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

Example

Example

5 | 4;   // 5

Behind the code

5 =(32 bit binary )=  00000000 00000000 00000000 00000101
4 = (32 bit binary)=  00000000 00000000 00000000 00000100
| = (ORoperation)  =  00000000 00000000 00000000 00000101
00000000 00000000 00000000 00000101  =(64 Bit Number) = 5
That's why 5 | 4  result to 5

3. Bitwise XOR operator (^)

To perform the XOR operation between two numbers we use the ^ symbol. The XOR operator returns 1 if both the bits are different else it returns 0.

XOR Table

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

Example

5 ^ 4;    //1

Behind the code

5 =(32 bit binary )=  00000000 00000000 00000000 00000101
4 = (32 bit binary)=  00000000 00000000 00000000 00000100
^ = (XOR operation)=  00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000001  =(64 Bit Number) = 1
That's why 5 ^ 4  result to 1

4. Bitwise NOT operator (~)

Bitwise NOT is a unary operator, it performs only on a single operad, and it inverts the bit data from 0 to 1 and 1 to 0.

NOT Table

Operation Result
~0 1
~1 0

Example

~5; //6

Behind the code

5 =(32 bit binary )=   00000000 00000000 00000000 00000101
~5 = (NOT operation)=  11111111 11111111 11111111 11111010
11111111 11111111 11111111 11111010  =(64 Bit Number) = -6
That's why ~5  result to -6

5. Bitwise Left shift (<<)

The Left shift operator <<< pushed the specified number of zero bits from the right side, which leads to a falloff of the leftmost bit.

Example

Let’s pushed 2 zero bits from the right side of 5.

5 << 2;  //20

Behind the code

5 =(32 bit binary )=   00000000 00000000 00000000 00000101
5<<2=(push 2 zeors )=  00000000 00000000 00000000 00010100
00000000 00000000 00000000 00010100  =(64 Bit Number) = 20
That's why 5<<2  result to 20

6. Bitwise Right shift (>>)

The Right shift operator shifts the specified number of bits to the right, this means it adds the specified number of zero bits from the left side of the bits.

Example

Let’s pushed 1 zero bits from the left side of 5.

5 >> 1;  //2

Behind the code

5 =(32 bit binary )=   00000000 00000000 00000000 00000101
5>>2=(push 1 zeors )=  00000000 00000000 00000000 00000010
00000000 00000000 00000000 00000010  =(64 Bit Number) = 2
That's why 5>>1 result to 2

7. Bitwise Zero Fill Right shift Operator (>>>)

The zero-fill right shift operator can push one or more zero bits from the left side of the binary bit number. It is quite similar to the right shift operator.

Example

Let’s pushed 1 zero bits from the left side of 5.

5 >>> 1;  //2

Behind the code

5 =(32 bit binary )=   00000000 00000000 00000000 00000101
5>>>2=(push 1 zeors )=  00000000 00000000 00000000 00000010
00000000 00000000 00000000 00000010  =(64 Bit Number) = 2
That's why 5>>>1 result to 2

Summary

• & (AND) operator performs AND operation between two binary bits.
• | (OR) operator performs OR operation between two binary bits.
• ^ (XOR) operator performs XOR operation between two binary bits.
• ~ (NOT) operator inverts the bits.
• << (Left Shift) operator shift the bits from the right side toward the left.
• >> (Right Shift) operator shift the bits from left side to right.
• The >>> (Zero fill right shift) shift the bits from left to right by filling zero. 