JavaScript Bitwise Operators
Last Updated :
23 Nov, 2024
In JavaScript, a number is stored as a 64-bit floating-point number but bitwise operations are performed on a 32-bit binary number. To perform a bit-operation, JavaScript converts the number into a 32-bit binary number (signed) and performs the operation and converts back the result to a 64-bit number.
List of Bitwise Operators with Explanation
1. Bitwise AND Operator ( & )
It is a binary operator i.e. accepts two operands. Bit-wise AND (&) returns 1 if both the bits are set ( i.e 1) and 0 in any other case.
JavaScript
let x = 5;
let y = 3;
console.log(x & y);
| A | B | OUTPUT ( A & B ) |
|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
2. Bitwise OR Operator ( | )
It is a binary operator i.e. accepts two operands. Bit-wise OR ( | ) returns 1 if any of the operands is set (i.e. 1) and 0 in any other case.
JavaScript
let x = 5;
let y = 3;
console.log(x | y);
| A | B | OUTPUT ( A | B ) |
|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
3. Bitwise XOR Operator ( ^ )
It is a binary operator i.e. accepts two operands. Bit-wise XOR ( ^ ) returns 1 if both the operands are different and 0 in any other case.
JavaScript
let x = 5;
let y = 3;
console.log(x ^ y);
| A | B | OUTPUT ( A ^ B ) |
|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
4. Bitwise NOT Operator ( ~ )
It is a unary operator i.e. accepts single operands. Bit-wise NOT ( ~ ) flips the bits i.e 0 becomes 1 and 1 becomes 0.
JavaScript
console.log(~10);
console.log(~-10);
5. Left Shift Operator ( << )
It’s a binary operator i.e. it accepts two operands. The first operator specifies the number and the second operator specifies the number of bits to shift. Each bit is shifted towards the left and 0 bits are added from the right. The excess bits from the left are discarded.
JavaScript
let a = 4;
console.log(a << 1);
console.log(a << 4);
| A | 6 ( 00000000000000000000000000000110 ) |
|---|
| B | 1 ( 00000000000000000000000000000001 ) |
|---|
| OUTPUT ( A << B ) | 12 ( 00000000000000000000000000001100 ) |
|---|
6. Sign Propagating Right Shift Operator ( >> )
It’s a binary operator i.e. it accepts two operands. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. This is Sign Propagating as the bits are added from the left depending upon the sign of the number (i.e. 0 if positive and 1 if negative )
JavaScript
let a = 4;
let b = -32
console.log(a >> 1);
console.log(b >> 4);
| A | 6 ( 00000000000000000000000000000110 ) |
|---|
| B | 1 ( 00000000000000000000000000000001 ) |
|---|
| OUTPUT ( A >> B ) | 3 ( 00000000000000000000000000000011 ) |
|---|
7. Zero Fill Right Shift Operator ( >>> )
It’s a binary operator i.e. it accepts two operands. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.
JavaScript
let a = 4;
let b = -1
console.log(a >>> 1);
console.log(b >>> 4);
| A | 6 ( 00000000000000000000000000000110 ) |
|---|
| B | 1 ( 00000000000000000000000000000001 ) |
|---|
| OUTPUT ( A >>> B ) | 3 ( 00000000000000000000000000000011 ) |
|---|
Recommended Links
JavaScript Operators Complete Reference
JavaScript Cheat Sheet.
Summary
JavaScript Bitwise Operators – FAQs
How does the & (AND) operator work?
The & operator performs a bitwise AND operation. It returns a binary number where each bit is 1 if the corresponding bits of both operands are 1, and 0 otherwise.
How does the | (OR) operator work?
The | operator performs a bitwise OR operation. It returns a binary number where each bit is 1 if at least one of the corresponding bits of the operands is 1.
How does the ^ (XOR) operator work?
The ^ operator performs a bitwise XOR (exclusive OR) operation. It returns a binary number where each bit is 1 if the corresponding bits of the operands are different, and 0 if they are the same.
How does the ~ (NOT) operator work?
The ~ operator performs a bitwise NOT operation. It inverts each bit of its operand, turning 0s into 1s and 1s into 0s.
How does the << (Left Shift) operator work?
The << operator shifts the bits of its left operand to the left by the number of positions specified by its right operand. Bits shifted out on the left are discarded, and zeros are shifted in from the right.
How does the >> (Sign-propagating Right Shift) operator work?
The >> operator shifts the bits of its left operand to the right by the number of positions specified by its right operand. Bits shifted out on the right are discarded. The leftmost bit (sign bit) is copied to fill the leftmost positions, preserving the sign of the number.
How does the >>> (Zero-fill Right Shift) operator work?
The >>> operator shifts the bits of its left operand to the right by the number of positions specified by its right operand. Bits shifted out on the right are discarded. Zeros are shifted in from the left, regardless of the sign of the original number.
What are some common uses of bitwise operators?
Bitwise operators are often used in low-level programming, such as:
- Setting, clearing, and toggling individual bits in a number.
- Performing bitwise manipulations for graphics and cryptography.
- Efficiently encoding and decoding data.
- Implementing bit masks to select specific bits from a binary value.
JavaScript
let a = 4;
let b = -1
console.log(a >>> 1);
console.log(b >>> 4);