Ternary numbers are a base 3 numeral system.
Base 10 is famously well suited to those of us who count on our fingers. Base 2 dominates computing technology because binary devices are simple and reliable, with just two stable states on or off. The cultural preference for base 10 and the engineering advantages of base 2 have nothing to do with any intrinsic properties of the decimal and binary numbering systems.
Base 3, on the other hand, does have a genuine mathematical distinction in its favor. By one plausible measure, it is the most efficient of all integer bases; it offers the most economical way of representing numbers as it is closer than binary to the most economical radix base(2.718) to represent arbitrary numbers, when economy is measured as the product of the radix and the number of digits needed to express a given range of values.
Truth value | Unsigned trit | Balanced trit |
---|---|---|
false | 0 | - |
unknown | 1 | 0 |
truth | 2 | + |
Balanced Ternary
The digits of a balanced ternary numeral are coefficients of powers of 3, but instead of coming from the set {0, 1, 2}, the digits are {-, 0 and +}. They are balanced because they are arranged symmetrically about zero.
Unsigned | Balanced | ||
---|---|---|---|
Dec. | Tern. | Tern. | Dec. |
0 | 00 | -- | -4 |
1 | 01 | -0 | -3 |
2 | 02 | -+ | -2 |
3 | 10 | 0- | -1 |
4 | 11 | 00 | 0 |
5 | 12 | 0+ | 1 |
6 | 20 | +- | 2 |
7 | 21 | +0 | 3 |
8 | 22 | ++ | 4 |
Arithmetic is nearly as simple as it is with binary numbers; in particular, the multiplication table is trivial. Addition and subtraction are essentially the same operation: Just negate one number and then add. Negation itself is also effortless: Change every N into a 1, and vice versa. Rounding is mere truncation: Setting the least-significant trits to 0 automatically rounds to the closest power of 3.
Encoding
Five trits can be stored in a byte by using modulo and division, this technique can store 243 possible values in a byte:
(n % 3) + '0', n /= 3;
If the number is encoded first into something like a float, it makes unpacking possible without division:
uint8_t q = (((uint16_t) i) * 256 + (243 - 1)) / 243; for (int j = 0; j < 5; ++j) { uint16_t m = q * 3; s2[j] = (m >> 8) + '0'; q = m & 0xFF; }
Perhaps the prettiest number system of all is the balanced ternary notation.Donald E. Knuth, The Art of Computer Programming
Notes on Ternary Logic.
Along with ternary arithmetic, a computer built of ternary hardware can also exploit ternary logic. Consider the task of comparing two numbers. In a machine based on binary logic, comparison is often a two-stage process. First you ask, "Is x less than y?"; depending on the answer, you may then have to ask a second question, such as "Is x equal to y?" Ternary logic simplifies the process: A single comparison can yield any of three possible outcomes: "less," "equal" and "greater."
Single Input Gates
The gate names uses the heptavintimal notation.
Reversible Gates
- Identity(U): The most dull gate (number 8) this does not change the input. It is its own complement. Applying it any number of times get you back to your intial value.
- Negation(E): Balanced ternary gates have a tighter relationship between logical and mathmatical negation. The are the same bitwise operator. It is its own complement. Applying it multiple time every even application brings back the intial value.
- Increment(G) and Decrement(K): These gates can arithmetically be thought of as single trit increment or decrement without carry, but with roll over. These gates are also complementary . Every 3 applications of either one of these gates in a row bring back the intial value.
- And gates O and S.
To learn more, see reversible computing.
+ | 0 | - | Number | Name | Definitions | |
---|---|---|---|---|---|---|
0 | - | - | - | -13 | False | NEG(LAX(LAX)) |
A | - | - | 0 | -12 | NEG(LAX) | |
B | - | - | + | -11 | Decoder(-) | DEC(DEC(LAX)) |
C | - | 0 | - | -10 | Abn | NEG(LAX(DEC)) |
D | - | 0 | 0 | -9 | DEC(LAX(NEG)) | |
E | - | 0 | + | -8 | Negation | NEG |
F | - | + | - | -7 | Decoder(0) | DEC(DEC(LAX(DEC))) |
G | - | + | 0 | -6 | Increment | DEC(DEC) |
H | - | + | + | -5 | DEC(NEG(LAX(NEG))) | |
I | 0 | - | - | -4 | NEG(LAX(NEG)) | |
J | 0 | - | 0 | -3 | DEC(LAX(DEC)) | |
K | 0 | - | + | -2 | Decrement | DEC |
L | 0 | 0 | - | -1 | DEC(LAX) | |
M | 0 | 0 | 0 | 0 | Clear | DEC(LAX(LAX)) |
N | 0 | 0 | + | 1 | NEG(DEC(LAX)) | |
O | 0 | + | - | 2 | NEG(DEC) | |
P | 0 | + | 0 | 3 | NEG(DEC(LAX(DEC))) | |
Q | 0 | + | + | 4 | LAX(NEG) | |
R | + | - | - | 5 | Floor/Decoder(+) | DEC(DEC(LAX(NEG))) |
S | + | - | 0 | 6 | DEC(NEG) | |
T | + | - | + | 7 | DEC(NEG(LAX(DEC))) | |
U | + | 0 | - | 8 | Identity | NEG(NEG) |
V | + | 0 | 0 | 9 | Flat | NEG(DEC(LAX(NEG))) |
W | + | 0 | + | 10 | Abs | LAX(DEC) |
X | + | + | - | 11 | Ceil | DEC(NEG(LAX)) |
Y | + | + | 0 | 12 | Lax | LAX |
Z | + | + | + | 13 | True | LAX(LAX) |
AND | B | |||
---|---|---|---|---|
F | U | T | ||
A | F | F | F | F |
U | F | U | U | |
T | F | U | T |
OR | B | |||
---|---|---|---|---|
F | U | T | ||
A | F | F | U | T |
U | U | U | T | |
T | T | T | T |
Inputs | Outputs | |||
---|---|---|---|---|
X | Y | TNAND | TNOR | |
−1 | −1 | 1 | 1 | |
−1 | 0 | 1 | 0 | |
−1 | 1 | 1 | −1 | |
0 | −1 | 1 | 0 | |
0 | 0 | 0 | 0 | |
0 | 1 | 0 | −1 | |
1 | −1 | 1 | −1 | |
1 | 0 | 0 | −1 | |
1 | 1 | −1 | −1 |
incoming ternary logic heptavintimal ternary computing terscii