JavaScript Introduction | Operators | Bitwise Operators
Bitwise Operators
Bitwise operators are similar to logical operators, but they perform logical operations at the bit level.
They are also used to move all bits to the left or right by a bit unit.
| Bitwise Operator | Description |
|---|---|
| & | Returns 1 when both corresponding bits are 1. (bitwise AND operation) |
| | | Returns 1 when at least one of the corresponding bits is 1. (bitwise OR operation) |
| ^ | Returns 1 when the corresponding bits are different. (bitwise XOR operation) |
| ~ | Reverses each bit: 1 becomes 0, and 0 becomes 1. (bitwise NOT operation) |
| « | Moves all bits to the left by the specified number. (left shift operation) |
| » | Moves all bits to the right by the specified number while preserving the sign. (right shift operation) |
| »> | Moves all bits to the right by the specified number, and all new bits become 0. |
Bitwise AND Operator (&)
The bitwise AND operator (&) returns 1 only when both corresponding bits are 1, and returns 0 in all other cases.
1-bit example
| AND Operator | Result |
|---|---|
| 0 & 0 | 0 |
| 0 & 1 | 0 |
| 1 & 0 | 0 |
| 1 & 1 | 1 |
4-bit example
| AND Operator | Result |
|---|---|
| 1111 & 0000 | 0000 |
| 1111 & 0001 | 0001 |
| 1111 & 0010 | 0010 |
| 1111 & 0100 | 0100 |
Bitwise OR Operator (|)
The bitwise OR operator (|) returns 1 when at least one of the two corresponding bits is 1, and returns 0 only when both bits are 0.
1-bit example
| OR Operator | Result |
|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
4-bit example
| OR Operator | Result |
|---|---|
| 1111 | 0000 | 1111 |
| 1111 | 0001 | 1111 |
| 1111 | 0010 | 1111 |
| 1111 | 0100 | 1111 |
Bitwise XOR Operator (^)
The bitwise XOR operator (^) returns 1 when the corresponding bits are different and returns 0 when they are the same.
1-bit example
| XOR Operator | Result |
|---|---|
| 0 ^ 0 | 0 |
| 0 ^ 1 | 1 |
| 1 ^ 0 | 1 |
| 1 ^ 1 | 0 |
4-bit example
| XOR Operator | Result |
|---|---|
| 1111 ^ 0000 | 1111 |
| 1111 ^ 0001 | 1110 |
| 1111 ^ 0010 | 1101 |
| 1111 ^ 0100 | 1011 |
Bitwise NOT Operator (~)
The bitwise NOT operator (~) returns 0 if the bit is 1, and returns 1 if the bit is 0.
1-bit example
| NOT Operator | Result |
|---|---|
| ~ 0 | 1 |
| ~ 1 | 0 |
4-bit example
| NOT Operator | Result |
|---|---|
| ~ 0000 | 1111 |
| ~ 0001 | 1110 |
| ~ 0010 | 1101 |
| ~ 0100 | 1011 |
Bit Shift Operators («, »)
The following example shows the shift operators («, ») and the bitwise NOT operator (~).
var x = 15, y = 8, z = 15;
document.write((x << 1) + "<br>"); // same as multiplying by 2, so 15 * 2 = 30
document.write((y >> 1) + "<br>"); // same as dividing by 2, so 8 / 2 = 4
document.write(~z); // same as one's complement, so -(15+1) = -16
In the example above, the first operation moves all bits to the left by one bit.
Therefore, the resulting value is the same as multiplying the original value by 2.
Conversely, the second operation moves all bits to the right by one bit.
Therefore, the resulting value is the same as dividing the original value by 2.