Bitwise operators in JavaScript are used to perform bitwise operations on the binary representations of integers. These operators work with the individual bits of an integer, rather than the value of the integer as a whole. This can be useful in some cases when working with low-level programming and data manipulation. In this article, we’ll cover the following bitwise operators in JavaScript:
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise NOT (~)
- Left shift (<<)
- Sign-propagating right shift (>>)
- Zero-fill right shift (>>>)
Bitwise AND (&)
The bitwise AND operator is represented by a single ampersand (&) symbol. It performs a bitwise AND operation on the binary representations of its two operands. The result of this operation is a new binary value where each bit is set to 1 only if the corresponding bits of both operands are also 1. If either bit is 0, the result bit will also be 0.
Example
let a = 5; // Binary: 0101
let b = 3; // Binary: 0011
let result = a & b; // Binary: 0001 (decimal: 1)
Bitwise OR (|)
The bitwise OR operator is represented by a single pipe (|) symbol. It performs a bitwise OR operation on the binary representations of its two operands. The result of this operation is a new binary value where each bit is set to 1 if either of the corresponding bits of the operands is 1. If both bits are 0, the result bit will also be 0.
Example:
let a = 5; // Binary: 0101
let b = 3; // Binary: 0011
let result = a | b; // Binary: 0111 (decimal: 7)
Bitwise XOR (^)
The bitwise XOR operator is represented by a single caret (^) symbol. It performs a bitwise XOR operation on the binary representations of its two operands. The result of this operation is a new binary value where each bit is set to 1 only if one of the corresponding bits of the operands is 1. If both bits are 0 or both bits are 1, the result bit will be 0.
Example
let a = 5; // Binary: 0101
let b = 3; // Binary: 0011
let result = a ^ b; // Binary: 0110 (decimal: 6)
Bitwise NOT (!)
The bitwise NOT operator is represented by a single tilde () symbol. It performs a bitwise NOT operation on the binary representation of its operand. The result of this operation is a new binary value where each bit is inverted (0 becomes 1, and 1 becomes 0).
Example
let a = 5; // Binary: 0101
let result = ~a; // Binary: 1010 (decimal: -6)
Left shift (<<)
The left shift operator is represented by two less than signs (<<) symbols. It performs a left shift operation on the binary representation of its first operand by the number of bits specified by the second operand. This effectively multiplies the first operand by 2 to the power of the second operand.
Example
let a = 5; // Binary: 0101
let result = a << 1; // Binary: 1010 (decimal: 10)
Bitwise XOR (^) Operator
The XOR operator (^) performs a bitwise exclusive OR operation on two operands. The result is 1 only if exactly one of the bits is 1, otherwise, the result is 0.
Example
const a = 5; // 0101
const b = 3; // 0011
const c = a ^ b; // 0110 (decimal 6)
In this example, the XOR operator returns 1 if the corresponding bits in the operands are different and 0 if they are the same. Therefore, the result of a ^ b
is 6 (binary 0110).
Bitwise NOT (~) Operator
The NOT operator (~) performs a bitwise complement operation on its single operand. It flips all the bits of the operand.
Example
const a = 5; // 0101
const b = ~a; // 1010 (decimal -6)
In this example, the NOT operator flips all the bits of the operand a
. Since a
is represented using 32 bits, the resulting number is -6
(binary 11111111111111111111111111111010) in two’s complement form.
FAQs
Q: What is the difference between the logical AND (&&) operator and the bitwise AND (&) operator?
A: The logical AND operator (&&) performs a Boolean comparison between two operands, whereas the bitwise AND operator (&) performs a binary comparison between the bits of the two operands.
Q: Can I use the bitwise operators with non-integer values?
A: No, the bitwise operators can only be used with integer values. If you try to use them with non-integer values, JavaScript will first convert the values to integers using the ToInt32
method.
Q: What is the difference between the left shift (<<) operator and the right shift (>>) operator?
A: The left shift operator (<<) shifts the bits of a number to the left, while the right shift operator (>>) shifts the bits of a number to the right. The left shift operator inserts 0 bits at the right end of the number, while the right shift operator inserts either 0 or 1 bits at the left end of the number, depending on the sign of the original number.
Q: What is the use of the bitwise NOT (~) operator?
A: The NOT operator flips all the bits of its operand, which can be useful for performing bitwise negation or complement operations. It can also be used to convert between two’s complement and unsigned integer representations.
Q: Are bitwise operations faster than other operations in JavaScript?
A: In general, bitwise operations are faster than other operations in JavaScript, but the difference in speed is usually not significant. Unless you are working with very large datasets or performance-critical code, you should not choose bitwise operations over other operations solely for their speed.