JavaScript Bitwise Operations
A number is stored in JavaScript as a 64-bit floating-point number, while a bit-wise operation, or performing a bit-operation, is performed on a 32-bit binary integer. JavaScript changes the number into a 32-bit signed binary number, performs the operation, and then changes the result back to the original 64-bit integer.
Operator | Name | Description |
---|---|---|
& | AND | if both bits are 1, sets each bit to 1. |
| | OR | if one of two bits is 1, it sets each bit to 1. |
^ | XOR | if just one of the two bits is 1, it sets each bit to 1. |
~ | NOT | Inverts all the bits. |
<< | Zero fill left shift | shifts left by inserting zeros from the right and letting the bits on the far left drop out. |
>> | Signed right shift | shifts right by allowing the rightmost bits to detach while pushing copies of the leftmost bit into the right from the left. |
>>> | Zero fill right shift | shifts right by allowing the rightmost bits to detach while zeros are pushed in from the left. |
JavaScript Uses 32 bits Bitwise Operands
Although JavaScript uses 64-bit floating-point integers for storage, all bitwise operations are carried out on 32-bit binary numbers. Numbers are converted to 32-bit signed integers in JavaScript before a bitwise operation is carried out. The result of the bitwise operation is transformed back to 64-bit JavaScript integers.
JavaScript Bitwise AND
When two bits are subjected to a bitwise AND, the result is 1 if both bits are 1.
Operation | Result |
---|---|
0 & 0 | 0 |
0 & 1 | 0 |
1 & 0 | 0 |
1 & 1 | 1 |
4 bits Example:
Operation | Result |
---|---|
Operation | Result |
1111 & 0000 | 0 |
1111 & 0001 | 1 |
1111 & 0010 | 10 |
1111 & 0100 | 100 |
JavaScript Bitwise OR
If one of the bits in a pair is 1, a bitwise OR on the pair of bits yields 1.
Operation | Result |
---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
4 bits Example:
Operation | Result |
---|---|
Operation | Result |
1111 | 0000 | 1111 |
1111 | 0001 | 1111 |
1111 | 0010 | 1111 |
1111 | 0100 | 1111 |
JavaScript Bitwise XOR
If two bits are subjected to a bitwise XOR and the bits are different, 1 is returned.
Operation | Result |
---|---|
0 ^ 0 | 0 |
0 ^ 1 | 1 |
1 ^ 0 | 1 |
1 ^ 1 | 0 |
4 bits Example:
Operation | Result |
---|---|
Operation | Result |
1111 ^ 0000 | 1111 |
1111 ^ 0001 | 1110 |
1111 ^ 0010 | 1101 |
1111 ^ 0100 | 1011 |
JavaScript Bitwise AND (&)
Only when both bits are 1 does bitwise AND return 1.
JavaScript Bitwise OR (|)
If one of the bits is 1, bitwise OR yields 1.
JavaScript Bitwise XOR (^)
If the bits are different, bitwise XOR gives a result of 1.
JavaScript (Zero Fill) Bitwise Left Shift (<<)
Left shift with zero fill. The leftmost bits drop off as one or more zero bits are put in from the right.
JavaScript (Sign Preserving) Bitwise Right Shift (>>)
This is a right shift-preserving symbol. The rightmost bits detach while copies of the leftmost bit are pushed in from the left.
JavaScript (Zero Fill) Right Shift (>>>)
It's a right shift with zero fill. The rightmost bits drop off as one or more zero bits are put in from the left.