detrytus

Counting in threes

A friendly introduction to balanced ternary, the way of writing numbers that the detrytus computer is built on. You will not need any maths beyond adding and subtracting.

Digits are switches

When you write a number like 307, each digit can be one of ten things, 0 to 9. We count in tens, most likely because we have ten fingers.

The computers you use every day count in twos. Deep inside, every digit is a tiny switch that is either off or on, written 0 or 1, and called a bit.

This site is about a computer that counts in threes, with a twist. Each of its digits is a switch with three positions: down, middle and up. Down means minus one, middle means zero, and up means plus one.

−1 down 0 middle +1 up
A trit is a switch with three positions.

A three-position digit is called a trit, short for "ternary digit". On this site the three positions are written − (down), 0 (middle) and + (up). The positions sit evenly around zero, one below and one above, which is why this is called balanced ternary.

Weighing with four weights

Here is an old puzzle. You have a balance scale and four weights: 1, 3, 9 and 27 kilos. Can you weigh every parcel from 1 to 40 kilos?

You can, as long as you may put weights in either pan. Each weight then has three choices:

? 3 9 1 parcel + 3 balances 9 + 1 so the parcel weighs 9 + 1 − 3 = 7
Weighing a 7 kilo parcel with the 9, the 3 and the 1.

To weigh a parcel of 7 kilos, put the 9 and the 1 opposite it and the 3 in beside it: 9 + 1 − 3 = 7. The 27 stays on the table.

Those three choices for each weight are exactly the three positions of a trit. Write down where each weight went, from the heaviest to the lightest, and you have written the number in balanced ternary:

Weight 27 9 3 1
Where it went table opposite with the parcel opposite
Trit 0 + − +

So 7 is written +−+ (the leading 0 can be dropped, just as nobody writes 007 for seven).

Reading a number

Each place is worth three times the place to its right: 1, 3, 9, 27, 81, and so on. To read a number, go along the places: add the place's value for a +, take it away for a −, and skip it for a 0.

27 9 3 1 + 0 − + add 27 skip take 3 add 1 27 + 0 − 3 + 1 = 25
+0−+ is 27 − 3 + 1, which is 25.

Here are the numbers from 0 to 13, written with three trits (the places 9, 3 and 1):

Number Trits Worked out
0 000
1 00+ 1
2 0+− 3 − 1
3 0+0 3
4 0++ 3 + 1
5 +−− 9 − 3 − 1
6 +−0 9 − 3
7 +−+ 9 − 3 + 1
8 +0− 9 − 1
9 +00 9
10 +0+ 9 + 1
11 ++− 9 + 3 − 1
12 ++0 9 + 3
13 +++ 9 + 3 + 1

Three trits can hold 3 × 3 × 3 = 27 different patterns. The table only shows 14 of them, from 0 to 13. The other 13 are the numbers from −1 down to −13, and they come for free.

Negative numbers for free

To make a number negative, flip every switch: up becomes down, down becomes up, and the middle stays where it is.

+ 0 − + = 25 flip every trit − 0 + − = −25
Flipping every trit turns 25 into −25.

That's all there is to it. There is no minus sign tacked onto the front, and no special rule for negative numbers: a number is negative when its first trit that isn't 0 is a −. Look at 2, written 0+−. Flip it and you get 0−+, which is −3 + 1 = −2.

Ordinary binary computers have it harder. They have to set aside a bit to say whether a number is negative, and play a clever trick (called "two's complement") so that adding and subtracting still work. A balanced-ternary computer needs none of that.

Rounding by chopping

In everyday numbers, chopping off the last digit always rounds down: 28 becomes 20, even though 30 is closer.

In balanced ternary, chopping trits off the end always rounds to the nearest value.

+ 0 − + + 0 − + 0 25 24 the nearest 3 27 the nearest 9
Chopping trits off 25 rounds it to the nearest 3, then to the nearest 9.

It works because the trits you chop off can never add up to more than half of a step, in either direction, so what is left is always the closest. For a computer this is a gift: rounding is one of the fiddliest things it does, and here it costs nothing at all.

Three answers to every comparison

Ask whether one number is bigger than another, and there are really three possible answers: smaller, the same, or bigger. A binary computer finds out with yes-or-no tests, often two of them. A single trit holds all three answers at once: −, 0 or +.

detrytus has an instruction that compares two numbers and gives back one trit, and another that jumps one of three ways depending on that trit.

Bigger numbers

Just as an ordinary computer groups 8 bits into a byte, detrytus groups 9 trits into a tryte. A byte has 256 possible values. A tryte has 19,683, from −9,841 to +9,841.

Three trytes make a word of 27 trits, enough for every whole number between roughly minus and plus 3.8 trillion.

Has anyone built one?

Yes. In 1958 a team at Moscow State University, led by Nikolay Brusentsov, built the Setun, a computer that worked in balanced ternary. About fifty were made, and a successor, the Setun-70, followed in 1970.

Since then almost every computer has been binary. A part with two states, off and on, is the simplest thing to build reliably out of electronics, and once binary had a head start, everything else was built around it.

detrytus asks a what-if question: what would a ternary computer look like if it had kept pace with fifty years of progress in ordinary ones? It exists as a detailed design and a program that pretends to be the machine, so it runs on an ordinary laptop. It even has its own operating system, trylobite, which is what sent you this page.

Where to go next