Module 08 · DSA · Worked problems, in English · about 17 min to read
Continue where you left off ×
Bits the hard way: XOR tricks and bit counting
Thinking in Bits · Open board
: a number as a row of switches, and the moves you can make on the row. This lesson uses those moves in three harder ways: counting switches column by column, walking a tree of switches, and using a whole row as one remembered set.
The idea in 30 seconds
A pair-cancelling XOR fails for triples, because three copies leave one behind.
Count each column of switches and keep the remainder mod 3, and the loner remains.
A bit trie stores numbers as switch paths, and a greedy walk finds the largest XOR in n × width steps.
A mask is a set stored as one number, bit i on meaning item i is used.
Mask plus a small value (leftover, or current place) is a state you can search once each.
State counts like 2ⁿ × n are the limit: fine for n = 12 to 20, hopeless for n = 40.
Read the story
Lesson 72 of 96 in DSA
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson When the pairs become triples Counting the switches in each column, then taking the rest after dividing by 3 Walking a tree of switches: the bit trie One number that remembers a set: the mask state A place and a mask together The column method, precisely The trie, precisely The mask DP, precisely The shortest-walk BFS, precisely The four solutions, run Where this shows up Terms it explains: three, column, column count mod 3, bit trie, mask state, leftover, (place, mask) state, Common mistakes.
Review DSA
Mark as read Saved in this browser, and to your account when you sign in.