Javascript Bitwise
JavaScript supports bitwise operations, which allow you to manipulate individual bits of a number at a low-level.
JavaScript internally stores numbers as 64-bit floating-point numbers using the IEEE 754 standard. However, when performing bitwise operations, JavaScript converts the numbers to 32-bit signed integers, performs the operation, and then converts the result back to a 64-bit floating-point number.
Following are the bitwise logical operator used in Javascript:-
Bitwise AND(&)
This bitwise operator 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 |
Following is the example to illustrate the use of AND operator:-
Input:-
[tryjavascript_Bitwise AND(&)]
Output:-
Bitwise OR(|)
This bitwise operator 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 |
Following is the example to illustrate the use of OR operator:-
Input:-
Output:-
Bitwise XOR(^)
This bitwise operator 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 |
Following is the example to illustrate the use of XOR operator:-
Input:-
Output:-
Bitwise NOT(~)
This bitwise operator 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 |
Following is the example to illustrate the use of NOT operator:-
Input:-
Output:-