Python – Bitwise Operators

Bitwise operators:

  • Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.
  • For example, 2 is 10 in binary and 7 is 111.
  • In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
OperatorMeaningExample
&Bitwise ANDx& y = 0 (0000 0000)
|Bitwise ORx | y = 14 (0000 1110)
~Bitwise NOT~x = -11 (1111 0101)
^Bitwise XORx ^ y = 14 (0000 1110)
>> Bitwise right shiftx>> 2 = 2 (0000 0010)
<< Bitwise left shiftx<< 2 = 40 (0010 1000)

Bitwise truth table:

>>> x=15
>>> y=8
>>> x&y
8
>>> x|y
15
>>> x^y

Scroll to Top