JavaScript Bitwise Operators
JavaScript bitwise operators allow you to perform operations on the binary representations of numbers. They work on 32-bit numbers at the binary level, manipulating bits directly. These operators are useful for low-level operations, such as optimizing performance or working with systems that require bit-level manipulation.
How Bitwise Operators Work
Before applying bitwise operations, JavaScript converts numbers into 32-bit binary. After performing the operation, the result is converted back to a standard JavaScript number.
1. & (AND)
The & operator compares each bit of two numbers and returns 1 only if both bits are 1.
Example:
1 2let result = 5 & 1; // Binary: 0101 & 0001 console.log(result); // Output: 1
Explanation:
0101(binary for5)0001(binary for1)0101 & 0001 = 0001(binary for1)
2. | (OR)
The | operator compares each bit of two numbers and returns 1 if at least one of the bits is 1.
Example:
1 2let result = 5 | 1; // Binary: 0101 | 0001 console.log(result); // Output: 5
Explanation:
0101(binary for5)0001(binary for1)0101 | 0001 = 0101(binary for5)
3. ~ (NOT)
The ~ operator inverts all the bits of the number (turns 0s to 1s and vice versa).
Example:
1 2let result = ~5; // Binary: ~0101 console.log(result); // Output: -6
Explanation:
0101(binary for5)~0101 = 1010(binary for-6in two's complement representation)
4. ^ (XOR)
The ^ operator compares each bit of two numbers and returns 1 if the corresponding bits are different.
Example:
1 2let result = 5 ^ 1; // Binary: 0101 ^ 0001 console.log(result); // Output: 4
Explanation:
0101(binary for5)0001(binary for1)0101 ^ 0001 = 0100(binary for4)
5. << (Left Shift)
The << operator shifts the bits of the number to the left by the specified number of positions, filling the vacated bits with 0.
Example:
1 2let result = 5 << 1; // Binary: 0101 << 1 console.log(result); // Output: 10
Explanation:
0101(binary for5)1010(binary for10after shifting left by 1)
6. >> (Right Shift)
The >> operator shifts the bits of the number to the right by the specified number of positions, filling the leftmost bits with the sign bit (0 for positive numbers, 1 for negative).
Example:
1 2let result = 5 >> 1; // Binary: 0101 >> 1 console.log(result); // Output: 2
Explanation:
0101(binary for5)0010(binary for2after shifting right by 1)
7. >>> (Unsigned Right Shift)
The >>> operator shifts the bits to the right, just like >>, but fills the leftmost bits with 0 regardless of the sign.
Example:
1 2let result = 5 >>> 1; // Binary: 0101 >>> 1 console.log(result); // Output: 2
Explanation:
0101(binary for5)0010(binary for2after unsigned right shift by 1)
Use Cases of Bitwise Operators
- Low-Level Programming: Useful in tasks like bit masking, setting specific bits, or clearing bits.
- Performance Optimization: Sometimes bitwise operations can offer faster alternatives to certain arithmetic operations.
- Graphics and Game Development: Often used in manipulating pixel data and optimizing graphical calculations.
Frequently Asked Questions
A bitwise operator in JavaScript works on the binary representations of numbers. It performs operations on individual bits, allowing manipulation of data at the bit level.
A bitwise operator performs operations on the binary representation of integers. For example, &, |, ^, ~, <<, >>, and >>> are common bitwise operators.
The &= operator is a bitwise AND assignment operator. It performs a bitwise AND operation between the left operand and the right operand, then assigns the result back to the left operand.
Yes, JavaScript has an XOR (exclusive OR) operator, represented by ^. It compares two bits and returns 1 if the bits are different, and 0 if they are the same.
Still have questions?Contact our support team