Generate a follow-up sub-lesson on any aspect of this topic
Guide complete
Nim Game Theory: Winning With XOR
Generate a follow-up sub-lesson on any aspect of this topic
Nim Game Theory: Winning With XOR
Learn to spot winning and losing Nim positions instantly using XOR, find the one move that forces a win, avoid the most common traps like misère endgames, and see how the same idea powers Sprague–Grundy analysis for many impartial games.
Nim feels like luck when you are guessing which heap to shrink. It stops feeling random the moment you treat each heap size as bits and combine them with exclusive OR (XOR). That one operation splits the entire game into positions that are dead cold and positions that are hot with a forced win. The punchline is simple and weird. If the XOR of all heap sizes is zero, you are losing with perfect play. If it is nonzero, you can always move to make it zero.
Players usually notice patterns that do not generalize, like mirroring moves or always taking the most. Nim punishes that. The game state is not about totals or symmetry. It is about which bit positions have odd parity across the heaps.
A useful mental model is this.
See how heap sizes line up in binary and where the nim-sum lights up.
When the highlighted nim-sum is all zeros, every bit column has even parity. Any move you make flips some columns from even to odd, giving your opponent a nonzero nim-sum to work with. When the nim-sum is nonzero, at least one highest bit is odd, and that is the handle you can grab to force the game back to zero.
Parity lensThink in bit columns. Nim is a parity game disguised as a pile-taking game.
A Nim position is losing under normal play if and only if the XOR of all heap sizes is zero. From every winning position, there exists a move to a losing position, meaning a move that makes the XOR zero.
That is Bouton’s theorem. The reason it is so powerful is that it gives you both classification and a constructive strategy.
Look at how moves from one sample position branch into resulting nim-sums.
If the nim-sum is zero, every bit column has an even number of 1s across heaps. When you reduce one heap, you change that heap’s binary representation. At the highest bit where it changes, you flip the parity of that column from even to odd. That guarantees the new nim-sum is nonzero.
If the nim-sum is nonzero, let be the highest bit position where it has a 1. In that column, there are an odd number of heaps with a 1. Pick one of those heaps. You can reduce it so that, after the reduction, its bits match the nim-sum pattern needed to make every column even again. That produces nim-sum zero in one move.
Computing the nim-sum is easy, but the real skill is turning it into the exact heap reduction quickly and reliably. The algorithm is deterministic.
Explore how changing heaps changes the binary view and the suggested reduction.
A quick sanity check helps. You are only allowed to reduce a heap, never increase it. The formula looks like magic, but it only works when you pick a heap with the highest 1-bit of set. That condition makes strictly smaller than , so the move is legal.
One heap changesA winning Nim move modifies exactly one heap. The rest stay unchanged, and the nim-sum becomes zero.
Many losses come from using rules that work in special cases and then failing quietly outside them.
Before you trust a heuristic, it should survive tiny counterexamples. The fastest way to build intuition is to see how small positions break common advice.
In normal play, taking the last object wins. In misère play, taking the last object loses. That single change makes endgames with heaps of size 1 behave differently, and it is where many people apply nim-sum and get burned.
Greedy play often hands over a zero nim-sum. If you take a lot from a heap without checking bits, you might accidentally give your opponent the exact cold position they want.
Mirroring works only when the game is structurally symmetric under the legal moves you can maintain. XOR symmetry is different. Two equal heaps do cancel in nim-sum, but that does not mean copying moves will preserve a winning structure when there are three or more heaps or mixed sizes.
Nim is the gateway example because its analysis is clean. The bigger idea is that many impartial games can be translated into Nim heaps whose sizes are not physical objects but computed values.
Compare how strategy changes across common variants and related games.
For misère Nim, nim-sum still guides play until the endgame where all heaps are size 1. In that all-ones phase, the winning condition flips. You want to leave an odd number of 1-heaps to your opponent, because they are forced to take the last one.
The Sprague–Grundy theorem says every impartial game position under normal play is equivalent to a Nim heap of some size called its Grundy number. Disjunctive sums of games combine by XOR of Grundy numbers, just like heap sizes in Nim. Nim is not just a game. It is the arithmetic of impartial games.
Same mathWhen a game is impartial and uses normal play, the combination rule is XOR, even if nothing looks like heaps.
Speed comes from reducing friction, not from memorizing tons of cases. You want nim-sum quickly, then the highest 1-bit, then the one heap to adjust.
A few practical shortcuts:
Practice quick XOR facts and small losing positions until they feel automatic.
The goal is not mental math perfection. It is confidence that you can spot positions and, when , identify one heap that must shrink.
Pick a real impartial game you like and try to turn positions into Grundy numbers. Start small. List the legal moves from each position, compute the mex, and watch Nim heaps appear where you did not expect them. Once you can do that for a toy game, Nim stops being a puzzle trick and becomes a reasoning tool you can reuse anywhere you see independent subgames that add together.