Set out several piles of stones, and two players take turns removing stones. There are only two rules: on each turn you can only take from one pile, and you're free to take anywhere from 1 stone up to the whole pile. Whoever takes the last stone wins. The rules are this simple, yet remarkably, there's a way to predict perfectly, before the game even starts, exactly who will win.

The secret is computing each pile's stone count using an operation called XOR (exclusive OR). XOR compares two numbers digit by digit in binary, and for each digit position, gives 0 if the digits match and 1 if they differ. For example, with piles of [3, 4, 5], in binary that's 3=011, 4=100, 5=101. Taking the XOR digit by digit: the first digit is 0⊕1⊕1=0, the second is 1⊕0⊕0=1, the third is 1⊕0⊕1=0. So the XOR value is 010, which is 2.

3 = 011 4 = 100 5 = 101 XOR = 010 = 2 0 if the digit matches,
1 if it differs
Convert each pile's count to binary and take the XOR digit by digit

If this XOR value (called the "Nim-sum") is not 0, then whoever's turn it is right now has a move that's guaranteed to win. Just XOR each pile's count with the XOR value itself, and find the pile where the result comes out smaller than the original count. With [3,4,5], the XOR was 2 — 5 XOR 2=7 and 4 XOR 2=6 both come out larger, so those don't work, but 3 XOR 2=1 comes out smaller, so that one works. So taking 2 stones from the first pile (of 3), leaving just 1, turns the piles into [1,4,5], and the XOR there becomes 1⊕4⊕5=0.

On the other hand, if the XOR is already 0, whoever's turn it is right now is at a disadvantage. As long as the opponent plays perfectly, whatever move you make will turn the XOR back to something other than 0, and the opponent will just keep turning it back to 0 every time, until eventually the opponent is the one who takes the last stone. So the winning strategy for Nim is remarkably simple — "always make sure the XOR is 0 right when your turn ends."

Why does XOR specifically make this work? XOR has the property of "canceling out things that pair up." When the game ends (every pile is at 0), the XOR is naturally 0 too. And from a state where the XOR is 0, changing just one pile is guaranteed to break it away from 0 (since changing only one pile throws off the balance). Conversely, when the XOR isn't 0, there's always some pile whose imbalance exactly cancels it out, letting you bring the XOR back to 0. These two properties fit together to make the winning strategy work: "keep handing the turn back to your opponent with XOR=0, and eventually they'll be cornered."

On our activity page, the computer plays using exactly this XOR strategy, so if the computer starts from an advantageous position, it's genuinely hard to beat. But use the hint button to check the XOR value and the recommended move at every turn, and you can beat the computer too. Try out different pile setups, and build the habit of checking right at the start whether the XOR is 0 (meaning you're at a disadvantage) or not.