← All articles

How to Win at Nim Duel: The XOR Trick That Makes You Unbeatable

Published 15 September 2026 · By SkillClash Team · 7 min read

Nim is a simple game. Three piles of stones — 3, 5, and 7. Take turns removing 1 or 2 stones from any single pile. Whoever takes the last stone wins.

Simple. But here's the thing that makes Nim special: the game has a perfect mathematical solution. If you know it, you can beat any opponent who doesn't — every single time.

This isn't a strategy article about tips and tricks. This is the actual math that makes Nim solvable. Learn it, and you'll never lose a Nim Duel on SkillClash again (against opponents who don't know it).

First: The Setup

Nim starts with three piles:

Pile 1: 3 stones
Pile 2: 5 stones
Pile 3: 7 stones

Players take turns removing 1 or 2 stones from any single pile. The player who takes the last stone wins.

Your turn. What do you do?

The Secret: XOR

The winning strategy in Nim is based on a mathematical operation called XOR (exclusive OR). It's the same operation used in computer logic — if you've ever programmed, you've seen it as ^.

Here's how it works in binary:

The key property: XOR is its own inverse. If you XOR a number with itself, you get zero. This is what makes Nim solvable.

Computing nimSum

The core of the strategy is a value called nimSum:

nimSum = pile1 XOR pile2 XOR pile3

Compute this at the start of every turn:

Example: The Opening Position

Piles: 3, 5, 7. Let's compute nimSum:

3 XOR 5 XOR 7 = ?
3 in binary = 0 1 1
5 in binary = 1 0 1
7 in binary = 1 1 1
─────────────
XOR result  = 0 0 1 = 1

nimSum = 1, which is not zero. You're in a winning position.

How to Exploit a Winning Position

To win, you need to make a move that leaves nimSum = 0 for your opponent. This forces them into a losing position.

Here's the method:

  1. For each pile, compute pile XOR nimSum
  2. Find the pile where the result is less than the current pile size
  3. Reduce that pile to the computed value

Example continued

Piles: 3, 5, 7. nimSum = 1.

All three are winning moves. You could reduce any of them. Let's pick pile 1 → reduce 3 to 2. Now piles are 2, 5, 7.

Check: 2 XOR 5 XOR 7 = 0. ✅ You've left your opponent in a losing position.

What Your Opponent Will Do

Your opponent faces piles 2, 5, 7 with nimSum = 0. Whatever move they make, they'll break the balance — leaving you a nimSum ≠ 0 position, which you can restore to zero on your next turn.

You just have to keep restoring the balance until you take the last stone.

The Full Strategy in 4 Steps

  1. Compute nimSum of all three piles at the start of your turn
  2. If nimSum = 0, you're in a losing position — play the best move you can and hope for a mistake
  3. If nimSum ≠ 0, find a pile where reducing it makes nimSum = 0
  4. Make that move. Repeat until you take the last stone.

The magic of Nim

Once you know this strategy, you can beat any opponent who doesn't — 100% of the time from a winning position. Nim isn't a game of intuition. It's a game of arithmetic.

Shortcut: Memoize Common Positions

You won't compute binary XOR in your head during a live match. Instead, memorize common positions:

Losing positions (nimSum = 0) — if you're here, play for a mistake

Winning positions (nimSum ≠ 0) — you can force a win

Any position not in the list above is a winning position. Find the move that leads to a losing position for your opponent.

The Opening Position Is Always Winning

SkillClash starts every Nim Duel with piles of 3, 5, 7. That's a winning position for whoever moves first. So going first is a big advantage.

The winning opening moves are: reduce pile 1 from 3 → 2, or pile 2 from 5 → 4, or pile 3 from 7 → 6. Any of those works.

Pick one, stick with it, and master the follow-ups. Most players won't know the XOR strategy, so you'll win almost every match.

Common Beginner Mistakes

  1. Playing randomly. Nim has a mathematical solution — you don't need to guess.
  2. Not computing nimSum. Every turn, compute the XOR. It takes 5 seconds.
  3. Failing to restore balance. Once you have a winning position, keep returning to nimSum = 0 after every move.
  4. Not memorizing common positions. The patterns repeat. Learn them, play faster.

Put It Into Practice

You know the math. Now you need to apply it in real matches. Play 20 games on SkillClash using the XOR method. By match 10, you'll be computing nimSum in your head automatically.

Most opponents won't know this strategy. You'll win far more than you lose.

Play Nim Duel on SkillClash →

Try These Other Strategy Games

Tic-Tac-Toe Connect 4 Dots & Boxes Memory Match Word Scramble All 21 games →