
Bitwise operators
TypeScript supports the following bitwise operators. To understand the examples, you must assume that variable A holds 2 as value and variable B holds 3 as value:
Operator |
Description |
Example |
& |
Known as the bitwise AND operator, it performs a boolean AND operation on each bit of its integer arguments. |
(A & B) is 2 |
| |
Known as the bitwise OR operator, it performs a boolean OR operation on each bit of its integer arguments. |
(A | B) is 3. |
^ |
Known as the bitwise XOR operator, it performs a boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. |
(A ^ B) is 1. |
~ |
Known as the bitwise NOT operator, it is a unary operator and operates by reversing all bits in the operand. |
(~B) is -4 |
<< |
Known as the bitwise shift-left operator. It moves all bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying by two, shifting two positions is equivalent to multiplying by four, and so on. |
(A << 1) is 4 |
>> |
Known as the bitwise shift-right with sign operator. It moves all bits in its first operand to the right by the number of places specified in the second operand. |
(A >> 1) is 1 |
>>> |
Known as the bitwise shift-right with zero operators. This operator is just like the >> operator, except that the bits shifted from the left are always zero. |
(A >>> 1) is 1 |