• In Python, bitwise operators are used to performing bitwise
calculations on integers. The integers are first converted into binary
and then operations are performed on bit by bit, hence the name
bitwise operators. Then the result is returned in decimal format.
• Note: Python bitwise operators work only on integers.
Bitwise Operators OPERATOR DESCRIPTION SYNTAX
& Bitwise AND x&y
| Bitwise OR x|y
~ Bitwise NOT ~x
^ Bitwise XOR x^y
>> Bitwise right shift x>>
<< Bitwise left shift x<<
• Let’s understand each operator one by one. Bitwise AND operator: Returns 1 if both the bits are 1 else 0. Example: a = 10 = 1010 (Binary) b = 4 = 0100 (Binary a & b = 1010 & 0100 = 0000 = 0 (Decimal) • Bitwise or operator: Returns 1 if either of the bit is 1 else 0. Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary
a | b = 1010 | 0100 = 1110 = 14 (Decimal)
Bitwise not operator: Returns one’s complement of the number.