Nim: Strategy

Xor

Xor (denoted with \oplus) is a binary operation (like addition or multiplication) that takes two numbers and returns a number. Imagine first that xor is only defined on 0 and 1, and that it is defined as follows:

aabbaba \oplus b
000
011
101
110

The table above shows the truth table for xor. The result of xor is 1 if the two bits are different, and 0 if they are the same. Now, we can extend this to larger numbers. The xor of two numbers is the xor of their binary representations, bit by bit.
For example:

There are a few more interesting properties of xor:

Strategy

What does this have to do with Nim? The xor of the sizes of the piles is called the nim-sum. The nim-sum is a powerful tool for determining the winning strategy in Nim.
Here's an example of the calculation of the nim-sum:

Determining winning and losing positions is simple:

Proof

We need to show two things:

  1. If the nim-sum is 00, then we can't make a move that will keep the nim-sum 00 (i.e. a losing position can't transition to another losing position).
  2. If the nim-sum is not 00, then we can make a move that will make the nim-sum 00 (i.e. a winning position can transition to a losing position).

Let's say that the nim-sum is xx. Suppose that we have a pile of size aa and we want to change it to bb. The nim-sum after the move will be xabx \oplus a \oplus b.

For the first part, we have x=0x = 0. We want to show that there is no bb such that xab=0x \oplus a \oplus b = 0. Since b<ab < a, aba \oplus b must not be 00 because the only way for aba \oplus b to be 00 is if a=ba = b.

For the second part, we have x0x \ne 0. We want to show that there is a bb such that xab=0x \oplus a \oplus b = 0. We can rearrange this to b=xab = x \oplus a by xor-ing both sides with bb. Since we need b<ab < a for this to be a valid move, we need to show that xa<ax \oplus a < a. Consider the binary representations of xx and aa. The xor of two numbers is the xor of their binary representations, bit by bit. The bit that determines whether xax \oplus a is greater than or less than aa is the most significant one bit (the leftmost one bit) of xx. Every bit to the left of it in xax \oplus a will be the same as in aa. Since the most significant one bit of xx is 11, that bit in aa will be toggled in xax \oplus a.
Consider the following examples that illustrate this:

Example 1:

xxaaxax \oplus a
011
000
011
101
000
110
101

Notice that the three leftmost bits of xax \oplus a are the same as in aa, but the next bit changes from 00 to 11. Therefore, xax \oplus a is greater than aa.

Example 2:

xxaaxax \oplus a
011
000
011
110
000
110
101

Notice that the three leftmost bits of xax \oplus a are the same as in aa, but the next bit changes from 11 to 00. Therefore, xax \oplus a is less than aa.

These examples show that the xax \oplus a is less than aa if the most significant one bit of xx is 11 in aa. Conversely, xax \oplus a is greater than aa if the most significant one bit of xx is 00 in aa.

Finally, this leads us to our optimal move. We can find a pile aa such that xa<ax \oplus a < a by finding a pile aa such that the most significant one bit of xx is 11 in aa. There will always be such a pile due to the properties of xor.