Algorithmsbitwise operations

Bitwise Operations for Students

TT
Testlaa Team
May 14, 20261 min read

Learn AND, OR, XOR, shifts, and masks—the primitives behind every bitmask trick.

Try this in Python

a, b = 0b1100, 0b1010
print(bin(a & b), bin(a | b), bin(a ^ b))
print(a << 1, a >> 1)
print(bin((1 << 4) - 1))  # low 4 bits set

Key takeaways

  • Precedence matters—parenthesize shifts with masks.
  • 1 << k singles out bit k.
  • Unsigned right shift is rare in Python contests.

Tags:

Bits & subset DPPythonStudents