๐ฎ The Nim Game: The Secret XOR Strategy That Guarantees You Win
Imagine playing a simple game.
๐ฎ The Nim Game: The Secret XOR Strategy That Guarantees You Win

Imagine playing a simple game.
There are a few piles of stones on a table:
Pile A: 3 stones
Pile B: 4 stones
Pile C: 5 stones
Two players take turns.
On each turn, a player can remove any number of stones from exactly one pile.
The player who removes the last stone wins.
The Question
Is this game just luckโฆ or can you guarantee a win?
At first glance, it feels random. You might try:
- Taking the biggest pile
- Removing as many stones as possible
- Or just โplaying smartโ
But none of that works consistently.
And yet:
There is a perfect strategy. And it always wins.
The Hidden Structure
This game is not about intuition.
It's about binary.
Let's rewrite the piles in binary:
3 = 011
4 = 100
5 = 101
Now compute their XOR:
011 โ 100 โ 101 = 010
That's 2 (non-zero)
The Rule That Changes Everything
Here is the entire secret:
โ If XOR = 0 โ Losing position โ If XOR โ 0 โ Winning position
That's it.
That one rule determines the entire game.
Why XOR?
XOR measures something subtle:
Balance
When XOR = 0:
- The piles are in a perfectly balanced state
- No matter what move you make, you break that balance
When XOR โ 0:
- The position is unbalanced
- You can always make a move to restore balance
The Winning Strategy
Step 1:
Compute XOR of all piles
Step 2:
If XOR โ 0 โ you are in a winning position
Step 3:
Make a move that makes:
New XOR = 0
Example (Winning Move)
We had:
3 โ 4 โ 5 = 2
Now your goal is to change one pile so the XOR becomes 0.
Let's try reducing 5:
3 โ 4 = 7
We want: 7 โ X = 0
So: X = 7
So instead of 5, we want 7 โ not possible.
Try another approach:
Target:
New value of pile = old XOR โ old pile
For pile 5:
New = 5 โ 2 = 7 โ (not smaller)
Try pile 3:
New = 3 โ 2 = 1 โ
So change pile A from 3 โ 1
New configuration:
1 โ 4 โ 5 = 0 โ
Perfect. You've forced a losing position on your opponent.
What Happens Next?
No matter what your opponent does:
- They must change XOR from 0 โ non-zero
- You can always bring it back to 0
Eventually:
- They are forced into the last move
- You win
The Deep Insight
This is the same idea you saw before โ but used differently.

Intuition (Without Math)
Think of XOR = 0 as perfect balance
Your goal is simple:
Always give your opponent a balanced position
Because from a balanced state:
- Every move creates imbalance
- And imbalance can always be fixed
You become the one controlling the system.
Why This Feels Like Magic
Because the winning strategy is invisible.
Two players can make identical-looking moves โ but:
- One is preserving balance
- The other is slowly losing control
The difference is not the moveโฆ
Itโs the structure behind the move
Real-World Connection
This isn't just a game.
The same idea powers:
โ Cryptography
- XOR used for secure encoding
โ Error correction
- Recover missing data in storage systems
โ AI & Game Theory
- Optimal decision strategies use hidden invariants
The One-Line Strategy
Calculate XOR. If itโs not zero, make it zero. Repeat until you win.
Final Thought
The Nim Game teaches something profound:
Winning isnโt about making clever moves. Itโs about maintaining the right invariant.
Once you see the pattern, the game stops being a game.
It becomes inevitable.
๋ฉํ๋ฐ์ดํฐ
- post_id
- 89a7551c4c4d
- slug
- the-nim-game-the-secret-xor-strategy-that-guarantees-you-win-89a7551c4c4d
- url
- https://medium.com/@johirbuet/the-nim-game-the-secret-xor-strategy-that-guarantees-you-win-89a7551c4c4d
- canonical_url
- https://medium.com/@johirbuet/the-nim-game-the-secret-xor-strategy-that-guarantees-you-win-89a7551c4c4d
- author_url
- https://medium.com/@johirbuet
- status
- ok
- fetched_at
- 2026-06-09 15:37:30