# Balanced Ternary

Do you have a favorite number system? I do. Knuth said:

Perhaps the prettiest number system of all, is the balanced ternary notation.

Balanced ternary is a number system where the radix is 3, but the digits are not 0, 1, and 2. Instead, it uses the digits -1, 0, and 1. This seems pretty strange at first, but it’s actually quite elegant. Let’s look at a few examples. To make things easy to read, I’m going to use the character ‘u’ to represent 1, the character ‘o’ to represent 0, and the character ‘n’ to represent -1.

uuo = 1*3^2 + 1*3^1 + 0*3^0 = 9 +3 +0 = 12

That seems reasonable enough. And, of course, thirteen is:

uuu = 1*3^2 +1*3^1 +1*3^0 = 9 +3 +1 = 13

But what comes next? It looks like this:

unnn = 1*3^3 -1*3^2 -1*3^1 -1*3^0 = 27 -9 -3 -1 = 14

OK, that’s a little odd, but it makes sense. Let’s look at another one.

nuuu = -1*3^3 + 1*3^2 + 1*3^1 + 1*3^0 = -27 +9 +3 +1 = -14

Cool, it can represent negative numbers without using a minus sign! And yes, negating a number is as simple as swapping all of the ‘n’ and ‘u’ characters. You can tell positive numbers from negative numbers by looking at the first non-zero digit. If it’s a ‘u’, then the number is positive. If it’s a ‘n’, then the number is negative.

If you’d like to learn more about balanced ternary, there are some good articles at American Scientist’s website, Wikipedia, and the Wiki-wiki.