Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Bit Manipulation Patterns
DSA

Bit Manipulation Patterns

Learn the core bitwise patterns used to solve efficient binary representation and XOR problems.

# Bit Manipulation Patterns

This file lists all commonly used bit manipulation patterns for DSA problems, along with typical problem types, keywords, and notes on how to detect and apply each pattern. Once internalized, these cues should immediately tell you which approach to use based on the input and question keywords.

Pattern Table (Simplified & Prioritized)

Pattern / CategoryTypical Question TypesKeywordsNotes / Scope / Rarity
XOR Patterns – Very Common (Arrays, Graphs, DP)Single number, Pair XOR, SubarraysXOR, unique, subsetCancel duplicates using XOR; prefix XOR for subarrays; universal usage
Count Set Bits / Parity – Very Common (Math, Arrays)Count 1s, Odd/even numbersbits, set bits, parityUse Brian Kernighan’s algorithm; O(log n) per number; universal math/array
Power of Two Checks – Common (Math, Arrays)Check if number is power of twon & (n-1), powerO(1) check using bitwise AND; frequent in constraints
Left / Right Shift Tricks – Common (Math, Arrays)Multiply/divide by powers of 2<<, >>Efficient arithmetic; often replaces multiplication/division
Bitmask for Subsets / Combinations – Rare / Bonus (Arrays, Strings, DP)Subset generation, Constraintsmask, subsetEncode subset selection as bits; iterate over all masks; used in combinatorial DP
XOR Graph Problems – Rare / Bonus (Graph only)XOR path problems, special constraintsXOR, path, edgesUse XOR along paths; often combined with DP or bitmask; graph-specific

Bit Manipulation – Detection & Usage Guide

Rarity Legend:

  • Very Common – Appears in almost every interview or coding contest; must know perfectly.
  • Common – Appears frequently; good to know for typical problems.
  • Rare / Bonus – Appears occasionally or in advanced/combinatorial problems.

1. XOR Patterns – Very Common

When to use / Detection cues:

  • Input: Array or sequence of integers.
  • Keywords: XOR, unique, subset.
  • Problem hints: Find the single non-repeating element, XOR of pairs, subarray XOR = k.
  • Why it works: XOR of a number with itself cancels to 0; XOR is associative; prefix XOR for subarrays.

Typical questions: Single non-repeating number, Count subarrays XOR=k, Two numbers with XOR=target. Mental trigger: “XOR” + “unique/single” → XOR Pattern.


2. Count Set Bits / Parity – Very Common

When to use / Detection cues:

  • Input: Integer numbers, usually small/moderate size.
  • Keywords: bits, set bits, parity, odd/even.
  • Problem hints: Count number of 1s, determine parity of number.
  • Why it works: Use Brian Kernighan’s algorithm (n & (n-1) removes the rightmost set bit); O(log n) per number.

Typical questions: Count 1s in integer array, Check parity of a number, XOR parity checks. Mental trigger: “Bits” + “count 1s / parity” → Count Set Bits / Parity.


3. Power of Two Checks – Common

When to use / Detection cues:

  • Input: Single integer.
  • Keywords: n & (n-1), power, check.
  • Problem hints: Constraint requires number to be a power of two, or check validity for combinatorial division.
  • Why it works: A number is power of 2 if it has exactly 1 bit set. n & (n-1) == 0.

Typical questions: Check if number is power of two, Validate constraints in array problems. Mental trigger: “n & (n-1)” → Power of Two check.


4. Left / Right Shift Tricks – Common

When to use / Detection cues:

  • Input: Integer numbers.
  • Keywords: <<, >>, multiply/divide by 2.
  • Problem hints: Need fast multiplication or division by powers of 2, or bitwise operations.
  • Why it works: Shifting left multiplies by 2, shifting right divides by 2; more efficient than arithmetic operators.

Typical questions: Multiply/divide integers by powers of 2, Optimize loops involving powers of 2. Mental trigger:<< / >>” → Multiply/divide by powers of 2.


5. Bitmask for Subsets / Combinations – Rare / Bonus

When to use / Detection cues:

  • Input: Array or set, small n (≤20).
  • Keywords: mask, subset, combination, constraints.
  • Problem hints: Generate all subsets or combinations efficiently; solve DP problems with subset states.
  • Why it works: Each bit represents inclusion/exclusion of an element; iterate through 0..2^n-1.

Typical questions: Subset sum variations, combinatorial constraints, DP over subsets. Mental trigger: “mask” + “subset / combination” → Bitmask DP.


6. XOR Graph Problems – Rare / Bonus

When to use / Detection cues:

  • Input: Tree or graph with weights.
  • Keywords: XOR, path, edges.
  • Problem hints: Find XOR along paths, constraints on XOR values between nodes.
  • Why it works: Use XOR properties (associative and commutative) combined with DP or DFS on graph.

Typical questions: XOR of path weights, Max XOR path in tree, Special constraints on node values. Mental trigger: “XOR” + “graph/path” → XOR Graph.

My Private Notes

Notes are auto-saved locally to this device.