What are different bitwise operators types in Python?

PythonServer Side ProgrammingProgramming

Bitwise operators operate upon bits as operands. Following bitwise operators are defined in Python −

  • & (bitwise AND): returns 1 if both bit operands are 1
  • | (bitwise OR): returns 1 even if one of two bit operands is 1
  • ^ (bitwise XOR): returns 1 only if one operand is 1 and other is 0
  • ~ (bitwise complement): returns 1 if operand is 0 and vice versa
  • << (bitwise left-shift): bits are shifted to left and right most bits are set to 0
  • >> (bitwise right-shift): bit are shifted to right and left most bits are set to 0

For example a = 60 (0011 1100 binary) and b = 13 (0000 1101 binary)

a&b = 0000 1100 = 12
a|b = 0011 1101 = 61
a^b = 0011 0001 = 49
~a = 1100 0011 = -61
a<<2 = 1111 0000 = 240
a>>2 = 0000 1111 = 15
raja
Published on 12-Feb-2018 00:02:23
Advertisements