Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Bit Manipulation/Bit Tricks & Bitmasking

Technique

XOR Linear Basis (Gaussian elimination over GF(2))

Reduce numbers to at most B independent vectors over GF(2) to answer max subset XOR, count distinct subset XORs, or membership.

Learn firstBit Tricks & Bitmasking
0%
mastery
0/5 solved
Practise nextMaximum Xor Subset
IdeaPracticePitfalls

Recognise it

The signal

maximum XOR of any SUBSET

  1. 1Brute force

    Enumerate all subsets.

  2. 2The observation

    2n subsets: the span of a basis of size <= B describes all of them.

  3. 3Why it works

    Basis vectors have distinct leading bits and span exactly the subset XORs seen so far.

Reach for it when

  • +number of distinct subset XOR values (2^rank)
  • +k-th smallest subset XOR

Not when

  • ×pairwise (not subset) XOR (binary-trie)

Practice

Next

Maximum Xor Subset

0/5 solved
  • Maximum Xor SubsetCSESHard
  • Number of Subset XorsCSESHard
  • K Subset XorsCSESHard
  • Square SubsetsCSESHard
  • Mahmoud and Ehab and yet another xor taskCodeforces2400

Pitfalls

Misconceptions

  • !confusing subset XOR with subarray XOR (prefix-xor + trie)
Go deeperresources

Resources

  • Bit manipulationcp-algorithms · article
  • Bit Twiddling HacksSean Anderson · article
  • Binary cheatsheetTech Interview Handbook · article
  • <bit> headercppreference · docs
TodayReviewPatternsProblems