JavaScript Bitwise operators
Bitwise operators in JavaScript perform operations on the binary representations of integers. They work at the bit level and can be useful for tasks involving low-level data manipulation. Here’s a detailed look at each bitwise operator:
1. Bitwise AND (&
)
- Purpose: Performs a binary AND operation. Each bit in the result is
1
if the corresponding bits in both operands are1
. - Example:
let a = 5; // Binary: 0101 let b = 3; // Binary: 0011 let result = a & b; // Binary: 0001 (Decimal: 1) console.log(result); // 1
2. Bitwise OR (|
)
- Purpose: Performs a binary OR operation. Each bit in the result is
1
if at least one of the corresponding bits in either operand is1
. - Example:
let a = 5; // Binary: 0101 let b = 3; // Binary: 0011 let result = a | b; // Binary: 0111 (Decimal: 7) console.log(result); // 7
3. Bitwise XOR (^
)
- Purpose: Performs a binary XOR operation. Each bit in the result is
1
if the corresponding bits in only one of the operands are1
. - Example:
let a = 5; // Binary: 0101 let b = 3; // Binary: 0011 let result = a ^ b; // Binary: 0110 (Decimal: 6) console.log(result); // 6
4. Bitwise NOT (~
)
- Purpose: Performs a binary NOT operation (bitwise complement). It inverts all the bits of the operand (flips
0
to1
and1
to0
). - Example:
let a = 5; // Binary: 0000 0101 let result = ~a; // Binary: 1111 1010 (Decimal: -6) console.log(result); // -6
- Note:
~5
equals-6
because bitwise NOT also flips the sign bit in two’s complement representation.
- Note:
5. Left Shift (<<
)
- Purpose: Shifts the bits of the first operand to the left by the number of positions specified by the second operand. Zeros are shifted into the vacated positions.
- Example:
let a = 5; // Binary: 0000 0101 let result = a << 1; // Binary: 0000 1010 (Decimal: 10) console.log(result); // 10
6. Sign-Propagating Right Shift (>>
)
- Purpose: Shifts the bits of the first operand to the right by the number of positions specified by the second operand. The sign bit (leftmost bit) is preserved.
- Example:
let a = -8; // Binary: 1111 1000 (in 8-bit two’s complement) let result = a >> 2; // Binary: 1111 1110 (Decimal: -2) console.log(result); // -2
7. Zero-Filling Right Shift (>>>
)
- Purpose: Shifts the bits of the first operand to the right by the number of positions specified by the second operand, filling in zeros from the left. This operation is unsigned.
- Example:
let a = -8; // Binary: 1111 1000 (in 8-bit two’s complement) let result = a >>> 2; // Binary: 0011 1110 (Decimal: 1073741822) console.log(result); // 1073741822
Key Points:
- Bitwise Operations: These operations work directly on the binary representations of numbers. For example,
5 & 3
compares the binary forms0101
and0011
, resulting in0001
. - Signed vs. Unsigned:
>>
maintains the sign bit for negative numbers, while>>>
does not, which means it always treats numbers as unsigned. - Efficiency: Bitwise operations are generally efficient and are used in scenarios where performance and memory are critical, such as in low-level programming, cryptography, and data processing.