← All Posts
DSA · Bit Manipulation· Part 31 of 32

Competitive Programming Bit Pattern Catalog

Advanced bit manipulation is mostly pattern recognition. You see constraints, operations, and required outputs, then map the problem to one of a small number of techniques. This catalog is the "which hammer do I reach for?" page.

Constraint Map

SignalLikely techniqueWhy
n <= 20, subsetsBitmask DP2^n states may fit.
All submasks for every maskSOS DPReplace O(3^n) with O(n * 2^n).
Pair result by XORBinary trie or FWTTrie for max/min/count with online values; FWT for full convolution.
Maximum subset XORXOR linear basisSubset XOR values form a vector space over GF(2).
Boolean DP with large sum/graph widthBitset optimizationProcess 64 booleans per CPU word.
Maximize answer bit-by-bitBitwise greedyHigher bits dominate lower bits if feasibility is monotonic.
Game with independent piles/componentsNim/Grundy XORIndependent impartial games combine by XOR.

XOR: Trie vs Basis vs Prefix

Bitmask DP Checklist

  1. What does one bit represent?
  2. What does dp[mask] mean exactly?
  3. Do transitions add one bit, remove one bit, split a mask, or merge submasks?
  4. Is the complexity O(n * 2^n), O(3^n), or worse?
  5. Can SOS DP, bitsets, or meet-in-the-middle reduce it?

Debugging Checklist

Interview vs CP Emphasis

Interviews usually emphasize clarity, invariants, and edge cases: single number, reverse bits, sum without plus, range AND, UTF-8 validation. CP pushes further into tries, SOS DP, FWT, linear basis, bitsets, and game XOR invariants.

Practice Problems