List of Bitwise Operators with Explanation
1. Bitwise AND Operator ( & )
It is a binary operator i.e. accepts two operands. Bit-wise AND (&) returns 1 if both the bits are set ( i.e 1) and 0 in any other case.
A | B | OUTPUT ( A & B ) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
2. Bitwise OR Operator ( | )
It is a binary operator i.e. accepts two operands. Bit-wise OR ( | ) returns 1 if any of the operands is set (i.e. 1) and 0 in any other case.
A | B | OUTPUT ( A | B ) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
3. Bitwise XOR Operator ( ^ )
It is a binary operator i.e. accepts two operands. Bit-wise XOR ( ^ ) returns 1 if both the operands are different and 0 in any other case.
A | B | OUTPUT ( A ^ B ) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
4. Bitwise NOT Operator ( ~ )
It is a unary operator i.e. accepts single operands. Bit-wise NOT ( ~ ) flips the bits i.e 0 becomes 1 and 1 becomes 0.
A | OUTPUT ( ~A ) |
---|---|
0 | 1 |
1 | 0 |
Below is an example of the JavaScript Bitwise Operators:
Example:
let a = 4;
let b = 1;
console.log("A & B = " + (a & b));
console.log("A | B = " + (a | b));
console.log("~A = " + (~a));
Output
A & B = 0 A | B = 5 ~A = -5
5. Left Shift Operator ( << )
It’s a binary operator i.e. it accepts two operands. The first operator specifies the number and the second operator specifies the number of bits to shift. Each bit is shifted towards the left and 0 bits are added from the right. The excess bits from the left are discarded.
A | 6 ( 00000000000000000000000000000110 ) |
---|---|
B | 1 ( 00000000000000000000000000000001 ) |
OUTPUT ( A << B ) | 12 ( 00000000000000000000000000001100 ) |
6. Sign Propagating Right Shift Operator ( >> )
It’s a binary operator i.e. it accepts two operands. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. This is Sign Propagating as the bits are added from the left depending upon the sign of the number (i.e. 0 if positive and 1 if negative )
A | 6 ( 00000000000000000000000000000110 ) |
---|---|
B | 1 ( 00000000000000000000000000000001 ) |
OUTPUT ( A >> B ) | 3 ( 00000000000000000000000000000011 ) |
7. Zero Fill Right Shift Operator ( >>> )
It’s a binary operator i.e. it accepts two operands. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.
A | 6 ( 00000000000000000000000000000110 ) |
---|---|
B | 1 ( 00000000000000000000000000000001 ) |
OUTPUT ( A >>> B ) | 3 ( 00000000000000000000000000000011 ) |
Example: Below is the implementation of the above-described operators.
let a = 6;
let b = 1;
// AND Operation
console.log("A & B = " + (a & b));
// OR operation
console.log("A | B = " + (a | b));
// NOT operation
console.log("~A = " + (~a));
// Sign Propagating Right Shift
console.log("A >> B = " + (a >> b));
// Zero Fill Right Shift
console.log("A >>> B = " + (a >>> b));
// Left Shift
console.log("A << B = " + (a << b));
Output:
A & B = 0
A | B = 7
~A = -7
A >> B = 3
A >>> B = 3
A << B = 12
Supported Browsers: The browsers supported by all JavaScript Bitwise operators are listed below.
- Google Chrome
- Internet Explorer
- Firefox
- Apple Safari
- Opera
We have a complete list of Javascript operators, to check those please go through this Javascript Operators Complete Reference article.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
JavaScript Bitwise Operators
JavaScript uses 32-bit Bitwise operands. A number is stored as a 64-bit floating-point number but the bit-wise operation is performed on a 32-bit binary number i.e. to perform a bit-operation JavaScript converts the number into a 32-bit binary number (signed) and performs the operation and converts back the result to a 64-bit number.