An explanation of all the bitwise operators available in JavaScript:
&
(Bitwise AND): Returns a1
in each bit position for which the corresponding bits of both operands are1
. Otherwise, it returns0
.
|
(Bitwise OR): Returns a1
in each bit position for which the corresponding bits of either or both operands are1
. Otherwise, it returns0
.
^
(Bitwise XOR): Returns a1
in each bit position for which the corresponding bits of either but not both operands are1
. Otherwise, it returns0
.
~
(Bitwise NOT): Inverts the bits of its operand. Returns a one's complement representation of the value.
<<
(Left Shift): Shifts the bits of its first operand to the left by the number of positions specified by the second operand. The leftmost bits are filled with0
.
>>
(Signed Right Shift): Shifts the bits of its first operand to the right by the number of positions specified by the second operand. The rightmost bits are filled with the sign bit (i.e. the most significant bit).
>>>
(Unsigned Right Shift): Shifts the bits of its first operand to the right by the number of positions specified by the second operand. The rightmost bits are filled with0
.
Note that the left shift (<<
) and right shift (>>
and >>>
) operators only work on integers (numbers without decimal points) in JavaScript. If you use these operators with non-integer numbers, the result will be truncated to an integer value before the shift is applied.