JavaScript Bitwise Operators (original) (raw)
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);
`
A | OUTPUT ( ~A ) |
---|---|
0 | 1 |
1 | 0 |
**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
OPERATOR NAME | USAGE | DESCRIPTION |
---|---|---|
Bitwise AND(&) | a & b | Returns true if both operands are true |
Bitwise OR(|) | a | b | Returns true even if one operand is true |
Biwise XOR(^) | a ^ b | Returns true if both operands are different |
Bitwise NOT(~) | ~ a | Flips the value of the operand |
Bitwise Left Shift(<<) | a << b | Shifts the bit toward the left |
Bitwise Right Shift(>>) | a >> b | Shifts the bit towards the right |
Zero Fill Right Shift(>>>) | a >>> b | Shifts the bit towards the right but adds 0 from left |