In bitwise operators, the operands are interpreted as a set of 32 bits (zeros and ones - short examples given in table below), as opposed to decimal, hexadecimal, or octal numbers. Bitwise operators return standard JavaScript numerical values, however.
Decimal: |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
4-bit binary: |
0000 |
0001 |
0010 |
0011 |
0100 |
0101 |
0110 |
0111 |
1000 |
1001 |
1010 |
With bitwise logical operators (&, |, ^), each operand is treated as a thirty-two-bit integer, and each bit in one operand is paired with the corresponding bit in the other. The operators are then applied to each pair of bits.
In the case of bitwise shift operators (<<, >>, >>>), the first operand is a quantity to be shifted, and the second specifies the number of bit positions by which the first operand is to be shifted. The operands are converted to thirty-two-bit integers for shifting, and the result is returned having the same type as the left operator.
Operator |
Name |
Description |
Example |
Result |
| a & b | Bitwise AND |
Returns a 1 in each bit position for which the corresponding bits of both operands are ones. |
10 & 6 | 2 |
| a | b | Bitwise OR |
Returns a 1 in each bit position for which the corresponding bits of either or both operands are ones. |
10 | 6 | 14 |
| a ^ b | Bitwise XOR |
Returns a 1 in each bit position for which the corresponding bits of either but not both operands are ones. |
10 ^ 6 | 12 |
| ~ a | Bitwise NOT |
Inverts the bits of its operand. |
~ 10 | -11 |
| a << b | Left shift |
Shifts |
10 << 2 | 40 |
| a >> b | Sign-propagating right shift |
Shifts |
7 >> 1 -7 >>1 |
3 -4 |
| a >>> b | Zero-fill right shift |
Shifts |
-7 >>>1 |
2147483644
|
Back to top Home Serious Stuff
© 2001. Copyright Sue Nethercott.