The Bitwise AND ( & )

The XOR ( ^ )

The OR ( | )

Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|

- bitwise AND operator
- bitwise OR operator
- bitwise XOR operator
- integer operand
- precedence
- associativity
- binary operator
- boolean
- The bitwise shift operators are covered separately. Although the shift operators are similar to the logical bitwise operators, the ECMAScript Language Specification addresses them separately. They also have a much higher precedence.
- Another member of the bitwise operators is the bitwise NOT operator. The ECMAScript Language Specification includes this operator with the unary operators. And this is where we choose to cover it also.
- Synonyms:
- Exclusive OR: means the same as the "XOR" notation.

This page will show the basic rules for the JavaScript bitwise operator group. Some special situation cases are explained. And finally, some scripting examples are provided with comments.

- The bitwise operators are binary operators. The two operands take integer values. They have a "left to right" associatively. The three binary bitwise operators each have a separate precedence level. The bitwise AND has the highest with level 8. The XOR follows with 7. And the OR has a precedence of 6.
- The bitwise operator group will return a value of type number. The two operands remain unchanged.
- The operands of the three bitwise operators should be integer numbers. For the bitwise operations, the numbers are represented as a 32-bit binary integer. The operators perform Boolean operations on corresponding bits of the two operands.
- The bitwise AND operator performs a Boolean AND operation on each bit of the two 32-bit integer operands. When corresponding bits in the operands are set to binary "1", then the resulting bit is set to "1".
- The bitwise OR operator performs a Boolean OR operation on each bit of the two 32-bit integer operands. When either of the corresponding bits in the operands are set to binary "1", then the resulting bit is set to "1".
- The bitwise XOR operator performs a Boolean XOR operation on each bit of the two 32-bit integer operands. When one but not both of the corresponding bits in the operands are set to binary "1", then the resulting bit is set to "1".
- The bitwise operators can be better visualized with the following truth table:

Bitwise Operator Truth Table Bit from 1st Operand Bit from 2nd Operand AND Result OR Result XOR Result 1 1 1 1 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0

- The bitwise operators will return unpredictable results when one or both of the operands are not integers.
- The bitwise operators will truncate any bits above 32 for integers that are too large to fit in a 32-bit representation (2147483648 and over).

We only diagram one byte in the following examples. Be aware that the bitwise operators work on 32-bit representations of the integer operands.

Expression | Giving | Comment |
---|---|---|

x = 11; x & 13; |
9 | bitwise AND 00001011 AND with 00001101 giving 00001001 |

x = 11; x | 13; |
15 | bitwise OR 00001011 OR with 00001101 giving 00001111 |

x = 11; x ^ 13; |
6 | bitwise XOR 00001011 XOR with 00001101 giving 00000110 |