Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
| Operator | Name | Description | Example | Try it |
|---|---|---|---|---|
| & | AND | Sets each bit to 1 if both bits are 1 | x & y | |
| | | OR | Sets each bit to 1 if one of two bits is 1 | x | y | |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ y | |
| ~ | NOT | Inverts all the bits | ~x | |
| << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | x << 2 | |
| >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | x >> 2 |
Examples
Example
print(6 & 3)
Example
print(6 | 3)
Example
print(6 ^ 3)