Practice Discrete Math

Algorithms / Bitwise Logic

Least You Need to Know: AND, OR, XOR, and Single-Bit Updates

Bitwise operators treat integers as compact boolean vectors. Interview problems use this to test parity, flip flags, cancel duplicates with XOR, and update one bit at a time in constant space.

The least you need to know

Key notation

x & mask keep only masked bits
x | mask set masked bits to 1
x ^ mask toggle masked bits

Tiny worked example

  • To force bit `k` on, OR with `1 << k`.
  • To force bit `k` off, AND with the inverse of `1 << k`.
  • To flip bit `k`, XOR with `1 << k`.
  • XOR also makes duplicate equal values cancel in pairing problems.

Common mistakes

How to recognize this kind of problem

Start practice