JavaScript bitwise operators treat their operands as a set of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. They perform operations on the binary representations, but they return standard JavaScript numerical values.
Assume variable A holds 2 (binary 0010) and variable B holds 3 (binary 0011):
| Operator | Name | Description | Example |
|---|---|---|---|
& |
Bitwise AND | Performs a Boolean AND operation on each bit of its integer arguments. | (A & B) is 2 |
| |
Bitwise OR | Performs a Boolean OR operation on each bit of its integer arguments. | (A | B) is 3 |
^ |
Bitwise XOR | Performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. | (A ^ B) is 1 |
~ |
Bitwise NOT | A unary operator that operates by reversing all the bits in the operand. | (~B) is -4 |
<< |
Left Shift | Moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. | (A << 1) is 4 |
>> |
Right Shift | Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand. | (A >> 1) is 1 |
>>> |
Right Shift with Zero | This operator is just like the >> operator, except that the bits shifted in on the left are always zero. |
(A >>> 1) is 1 |
&)The bitwise AND (&) operator returns a 1 in each bit position for which the corresponding bits of both operands are 1.
a & b
let a = 2; // Binary: 0010 let b = 3; // Binary: 0011let result = a & b; // Binary: 0010 console.log("(a & b) => " + result); // Outputs: 2
|)The bitwise OR (|) operator returns a 1 in each bit position for which the corresponding bits of either or both operands are 1.
a | b
let a = 2; // Binary: 0010 let b = 3; // Binary: 0011let result = a | b; // Binary: 0011 console.log("(a | b) => " + result); // Outputs: 3
^)The bitwise XOR (^) operator returns a 1 in each bit position for which the corresponding bits of either but not both operands are 1.
a ^ b
let a = 2; // Binary: 0010 let b = 3; // Binary: 0011let result = a ^ b; // Binary: 0001 console.log("(a ^ b) => " + result); // Outputs: 1
~)The bitwise NOT (~) operator reverses the bits of its operand. It transforms 0 to 1 and 1 to 0.
~a
let b = 3; // Binary: 00000000000000000000000000000011let result =
b; // Binary: 11111111111111111111111111111100 console.log("(b) => " + result); // Outputs: -4
<<)The left shift (<<) operator moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.
a << b
let a = 2; // Binary: 0010let result = a << 1; // Binary: 0100 console.log("(a << 1) => " + result); // Outputs: 4
>>)The binary right shift (>>) operator moves the left operand’s value right by the number of bits specified by the right operand. The sign bit is preserved in this operation.
a >> b
let a = 2; // Binary: 0010let result = a >> 1; // Binary: 0001 console.log("(a >> 1) => " + result); // Outputs: 1
>>>)The right shift with zero (>>>) operator is just like the >> operator, except that the bits shifted in on the left are always zero. This is also called Unsigned Right Shift.
a >>> b
let a = 2; // Binary: 0010let result = a >>> 1; // Binary: 0001 console.log("(a >>> 1) => " + result); // Outputs: 1
Which bitwise operator reverses all the bits in the operand?