On this page
Nim looks like a children's game of matchsticks, yet it has a complete mathematical solution that is more than a century old. Charles L. Bouton named the game and published its full theory in 1901, in a paper in the Annals of Mathematics titled Nim, a Game with a Complete Mathematical Theory. The trick needs nothing harder than binary.
What are the rules of Nim?
Two players face a few heaps of objects. On your turn you take at least one object, and as many as you like, from exactly one heap. In the normal version, the player who takes the last object wins; in the misère version, that player loses.
What is the nim-sum?
The nim-sum is the binary sum of the heap sizes with every carry thrown away, which is the same operation as bitwise XOR, and combinatorial game theory writes it as x ⊕ y. Write each heap in binary, line up the columns and count the 1s in each: odd gives 1, even gives 0.
Take heaps of 3, 4 and 5:
3 is 011
4 is 100
5 is 101
The fours column has two 1s (0), the twos column one (1), the ones column two (0). The nim-sum is 010, which is 2.
Rusty on binary? See our guide to binary addition and conversion by hand.
How do you win at the game of Nim?
Bouton's result: the player about to move has a winning strategy if and only if the nim-sum of the heap sizes is not zero. So your whole job is to finish every turn leaving a nim-sum of zero. To find that move:
Compute X, the nim-sum of all the heaps.
For each heap h, compute h ⊕ X.
Choose a heap where h ⊕ X is smaller than h.
Reduce that heap to exactly h ⊕ X.
With heaps 3, 4, 5 and X = 2: 3 ⊕ 2 = 1 is smaller, but 4 ⊕ 2 = 6 and 5 ⊕ 2 = 7 are larger. So take 2 from the heap of 3, leaving 1, 4, 5. Check: 001 ⊕ 100 ⊕ 101 = 000.
Here is a full worked game. Asha moves first against Rohan, from heaps A = 3, B = 4, C = 5.
Asha sees nim-sum 2. She takes 2 from A, leaving (1, 4, 5), nim-sum 0.
Rohan takes 3 from C, leaving (1, 4, 2). Nim-sum: 001 ⊕ 100 ⊕ 010 = 111, which is 7.
Asha checks: 4 ⊕ 7 = 3, smaller than 4. She reduces B to 3, leaving (1, 3, 2), nim-sum 0.
Rohan takes all of B, leaving (1, 0, 2). Nim-sum 3.
Asha checks: 2 ⊕ 3 = 1. She reduces C to 1, leaving (1, 0, 1), nim-sum 0.
Rohan must take one of the two single objects.
Asha takes the last object and wins.
Every time Rohan moved, he faced a zero. He made no mistake; there was no good move to find.
Why is a zero nim-sum losing for the player to move?
Two facts do all the work (a step-by-step proof of Bouton's result):
From zero, every move breaks it. A move changes one heap from x to a smaller y, so the new nim-sum is 0 ⊕ x ⊕ y = x ⊕ y, which is zero only if x equals y. It never is.
From non-zero, some move restores it. An odd number of heaps have a 1 in the column of X's leftmost 1, so at least one heap does. XOR-ing that heap with X clears that bit and leaves higher columns alone, so the heap shrinks and the move is legal. The new nim-sum is X ⊕ X = 0.
The game ends with every heap empty, which is nim-sum 0. Only the player who keeps leaving zeros can leave that final zero, so they take the last object.
What changes in misère Nim?
In misère play, the strategy differs only when the normal-play move would leave nothing but heaps of size one: then you leave an odd number of single heaps instead of an even number. Before that, play exactly as above.
How to practise it
Set out heaps of 1, 4 and 5 coins and offer a friend or parent the first move. They face a zero, so they cannot win while you keep restoring it. Then try 3, 5, 7 and work out who should move first.
XOR is a binary operation in its own right, with 0 as its identity and every number its own inverse; see closure and identity for binary operations. For more puzzles built on invariants and parity, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



