JavaScript bitwise operators are used to perform operations on binary representations of numbers. While these operators are not as commonly used as arithmetic or logical operators, they are crucial for tasks that require low-level manipulation of data.
Types of Bitwise Operators
JavaScript supports the following bitwise operators:
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise NOT (~)
- Bitwise LEFT SHIFT (<<)
- Bitwise RIGHT SHIFT (>>)
- Bitwise ZERO-FILL RIGHT SHIFT (>>>)
1. Bitwise AND (&) Operator
The bitwise AND operator (&) compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
console.log(5 & 3); // 1
// Explanation:
// 5: 0101
// 3: 0011
// --------
// &: 0001 (1 in decimal)
2. Bitwise OR (|) Operator
The bitwise OR operator (|) compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
console.log(5 | 3); // 7
// Explanation:
// 5: 0101
// 3: 0011
// --------
// |: 0111 (7 in decimal)
3. Bitwise XOR (^) Operator
The bitwise XOR operator (^) compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
console.log(5 ^ 3); // 6
// Explanation:
// 5: 0101
// 3: 0011
// --------
// ^: 0110 (6 in decimal)
4. Bitwise NOT (~) Operator
The bitwise NOT operator (~) inverts the bits of its operand, turning 1s into 0s and 0s into 1s. It is equivalent to the negation of the number plus one, also known as the two's complement.
console.log(~5); // -6
// Explanation:
// 5: 0101
// ~5: 1010 (which is -6 in two's complement)
5. Bitwise LEFT SHIFT (<<) Operator
The bitwise left shift operator (<<) shifts the bits of its first operand to the left by the number of positions specified by its second operand. Zero bits are shifted into the lower-order positions, and the bits shifted out of the higher-order positions are discarded.
console.log(5 << 1); // 10
// Explanation:
// 5: 0101
// 5<<1:1010 (which is 10 in decimal)
6. Bitwise RIGHT SHIFT (>>) Operator
The bitwise right shift operator (>>) shifts the bits of its first operand to the right by the number of positions specified by its second operand. Bits shifted out of the lower-order positions are discarded, and the higher-order positions are filled based on the sign bit (the leftmost bit).
console.log(5 >> 1); // 2
// Explanation:
// 5: 0101
// 5>>1:0010 (which is 2 in decimal)
7. Bitwise ZERO-FILL RIGHT SHIFT (>>>) Operator
The bitwise zero-fill right shift operator (>>>) shifts the bits of its first operand to the right by the number of positions specified by its second operand. Bits shifted out of the lower-order positions are discarded, and zero bits are shifted into the higher-order positions.
console.log(5 >>> 1); // 2
// Explanation:
// 5: 0101
// 5>>>1:0010 (which is 2 in decimal)
console.log(-5 >>> 1); // 2147483645
// Explanation:
// -5: 11111111111111111111111111111011
// -5>>>1: 01111111111111111111111111111101
// (2147483645 in decimal)