Operators

bayhiker

Content

Bitwise Operators

  • bin() function converts a number to its binary representation. bin(2) = 0b10
  • &: bitwise and: 2 & 3 = 0b10 & 0b11 = 0b10 = 2
  • |: bitwise or (|): 2 | 3 = 0b10 | 0b11 = 0b11 = 3
  • ~: bitwise not, also called 1's complement: For a 4-byte integer ~2 = ~0x0002 = 0xFFFD (-3)
  • ^: xor, 2 ^ 3 = 0b10 ^ 0b11 = 0b01
  • <<: bitwise shift left, 2 << 1 = 0b0010 << 1 = 0b0100 = 4
  • >>: bitwise shift right, 2 >> 1 = 0x0010 >> 1 = 0b0001 = 1