JavaScript Bitwise Operators
Bitwise operators are used to perform operations on the binary representations of numbers. In JavaScript, bitwise operators work on 32-bit signed integers. This article explores the various bitwise operators available in JavaScript, providing explanations and examples for each.
Types of Bitwise Operators
JavaScript includes several bitwise operators, each serving a different purpose. Here’s a breakdown of these operators:
1. Bitwise AND (&)
The bitwise AND operator performs a bitwise AND operation on two operands, returning a number that has bits set to 1 where both operands have bits set to 1.
let a = 5; // 0101 in binary
let b = 3; // 0011 in binary
console.log(a & b); // Output: 1 (0001 in binary)
2. Bitwise OR (|)
The bitwise OR operator performs a bitwise OR operation on two operands, returning a number that has bits set to 1 where either operand has bits set to 1.
let a = 5; // 0101 in binary
let b = 3; // 0011 in binary
console.log(a | b); // Output: 7 (0111 in binary)
3. Bitwise XOR (^)
The bitwise XOR operator performs a bitwise exclusive OR operation on two operands, returning a number that has bits set to 1 where the operands differ.
let a = 5; // 0101 in binary
let b = 3; // 0011 in binary
console.log(a ^ b); // Output: 6 (0110 in binary)
4. Bitwise NOT (~)
The bitwise NOT operator inverts all the bits of its operand, flipping 0s to 1s and 1s to 0s.
let a = 5; // 0101 in binary
console.log(~a);
// Output: -6
// (111...1010 in binary, 2's complement representation)
5. Bitwise Left Shift (<<)
The bitwise left shift operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. This is equivalent to multiplying the number by 2 to the power of the shift amount.
let a = 5; // 0101 in binary
console.log(a << 1); // Output: 10 (1010 in binary)
6. Bitwise Right Shift (>>)
The bitwise right shift operator shifts the bits of the left operand to the right by the number of positions specified by the right operand. This is equivalent to dividing the number by 2 to the power of the shift amount, rounding towards zero.
let a = 5; // 0101 in binary
console.log(a >> 1);
// Output: 2 (0010 in binary)
7. Bitwise Unsigned Right Shift (>>>)
The bitwise unsigned right shift operator shifts the bits of the left operand to the right by the number of positions specified by the right operand, filling in 0s from the left regardless of the sign of the original number.
let a = -5;
// 11111111 11111111 11111111 11111011 (binary for -5)
console.log(a >>> 1);
// Output: 2147483645
// 01111111 11111111 11111111 11111101 (binary)
Using Bitwise Operators
Bitwise operators are often used in low-level programming and performance-critical applications. They can be useful for tasks such as:
- Flag Management: Using bits as flags to represent multiple boolean values in a single integer.
- Masking: Applying masks to isolate specific bits within a binary number.
- Performance Optimization: Performing fast calculations and manipulations on integer values.
Conclusion
Understanding bitwise operators can be valuable for certain programming tasks, especially when working with binary data or optimizing performance. By leveraging these operators, you can perform efficient bit-level operations and manage data more effectively in JavaScript.
Support Our Work: BuyMeACoffee
Originally published at https://www.rustcodeweb.com on August 26, 2024.