JavaScript » Operators » Bitwise

Bitwise operators perform boolean and shift operations. They work by treating their operands as a series of 32 bits and performing their operations on them at this bit level.

The following examples of bitwise boolean operators assume the variable 'a' to be 13 (binary 1101) and 'b' to be 9 (binary 1001).

In the shift operation examples the variable 'a' is assumed to be 13 (binary 1101) and the variable 'b' 2 (binary 10).

&

This is the bitwise AND operator which returns a 1 for each bit position where the corresponding bits of both its operands are 1.

<<

This is the left shift operator and it works by shifting the digits of the binary representation of the first operand to the left by the number of places specified by the second operand.

>>

This is the sign-propagating right shift operator which shifts the digits of the binary representation of the first operand to the right by the number of places specified by the second operand, discarding any shifted off to the right.

>>>

This is the zero-fill right shift operator which shifts the binary representation of the first operand to the right by the number of places specified by the second operand.

^

This is the bitwise XOR operator, which returns a one for each position where one (not both) of the corresponding bits of its operands is a one.

|

This is the bitwise OR operator and returns a one for each bit position where one or both of the corresponding bits of its operands is a one.

~

This is the bitwise NOT operator and it works by converting each bit of its operand to its opposite.