JavaScript bitwise operators
Operators | name | description |
---|---|---|
& | Bitwise AND | If both bits are 1, then each bit is set to 1 |
I | Bitwise or | If the two bits to 1, each bit is set to 1 |
^ | Bitwise XOR | If only one of the two bits to 1, each bit is set to 1 |
~ | non- | Reverse all the bits |
<< | Zero padding to the left | By pushing from the right side to the left of zero, so that the leftmost digit fall |
>> | Arithmetic shift right | Moved by pushing a copy of the leftmost position from the left to the right, and the rightmost bit off |
>>> | Zero fill right shift | By moving from the left to the right push to zero, and let the rightmost bit off |
example:
operating | result | Equivalence | result |
---|---|---|---|
5 & 1 | 1 | 0101 & 0001 | 0001 |
5 L1 | 5 | 0101 I 0001 | 0101 |
~ 5 | 10 | ~0101 | 1010 |
5 << 1 | 10 | 0101 << 1 | 1010 |
5 ^ 1 | 4 | 0101 ^ 0001 | 0100 |
5 >> 1 | 2 | 0101 >> 1 | 0010 |
5 >>> 1 | 2 | 0101 >>> 1 | 0010 |
Bitwise AND
When the pair of bits and performs a bitwise operation, if the two bits are 1, 1 is returned.
An operation example:
operating | result |
---|---|
0&0 | 0 |
0&1 | 0 |
1&0 | 0 |
1&1 | 1 |
Bitwise or
When the bit pair or bit operation performed when, if at least one bit is 1, 1 is returned, otherwise 0:
An operation example:
operating | result |
---|---|
0I0 | 0 |
0I1 | 1 |
1I0 | 1 |
1I1 | 1 |
Bitwise XOR
When the pair of bit perform bitwise XOR operation, if bit different (or rather distinct) Returns 1:
An operation example:
operating | result |
---|---|
0^0 | 0 |
0^1 | 1 |
1^0 | 1 |
1^1 | 0 |
More comprehensive JavaScript operator Advanced Applications