-0+
A computer with word-sizes that should be measured in trits, not bits. We will use a 3-trit tribble, a 9-trit tryte, and a 27-trit word. Kleene logic augments the conventional true and false of Boolean logic with a third value, unknown.
| Truth value | Unsigned | Balanced | BCB |
|---|---|---|---|
| false | 0 | - | 11 |
| unknown | 1 | 0 | 00 |
| truth | 2 | + | 01 |
Binary-coded Balanced
Binary-coded balanced(BCB) is a representation allowing binary computers to manipulate ternary data. In ternary, one trit encodes 3 values. As such, if we use 2 bits per binary-encoded ternary trit.
| Unsigned | BCB | Balanced | ||
|---|---|---|---|---|
| Dec. | Tern. | Tern. | Dec. | |
| 0 | 00 | 1111 | -- | -4 |
| 1 | 01 | 1100 | -0 | -3 |
| 2 | 02 | 1101 | -+ | -2 |
| 3 | 10 | 0011 | 0- | -1 |
| 4 | 11 | 0000 | 00 | 0 |
| 5 | 12 | 0001 | 0+ | 1 |
| 6 | 20 | 0111 | +- | 2 |
| 7 | 21 | 0100 | +0 | 3 |
| 8 | 22 | 0101 | ++ | 4 |
The advantage of BCB over Douglas W.
Jones's BCT is that it makes detecting carry easier, due to
1+1=2 and 2+0=2 giving the same result, whereas if
you add two values that are 0, 1, and 3 you will never get the same sum two
different ways. To convert to the unsigned form, you can do
(x+1)>>1, and the reverse (x>>1)|x.
| Balanced Trite | |||
|---|---|---|---|
| tri | - | 0 | + |
| bc1 | 11 | 00 | 01 |
| int | -1 | 0 | 1 |
| Balanced Tribble | |||
| try | --- | 000 | +++ |
| bc3 | 111111 | 000000 | 010101 |
| int | -13 | 000 | 13 |
Tryte in 16-bit machines
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;
}
Heptavintimal on 32-bit machines
To convert and print the heptavintimal digits of 5 trybbles from a decimal integer stored in a 16-bit address space where each trit takes 2 bits:
typedef uint32_t trint16_t;
int
ter2bin(trint16_t t)
{
int sft = 1, acc = 0;
while(t)
acc += sft * (t & 0x3), t >>= 2, sft *= 3;
return acc;
}
trint16_t
bin2ter(int n)
{
trint16_t sft = 0, acc = 0;
while(n > 0)
acc |= (n % 3) << sft, n /= 3, sft += 2;
return acc;
}
trint16_t
hep2ter(char *str)
{
char c;
trint16_t acc = 0;
while((c = *str++))
acc <<= 6, acc |= bin2ter(c ? (c - 'A') + 1 : 0);
return acc;
}
void
print_heptavintimal(trint16_t n)
{
int i;
for(i = 4; i > -1; --i) {
int t = ter2bin((n >> (i * 6)) & 0x3f);
putchar(t ? '@' + t : '0');
}
}
// heptavintimal to decimal
printf("%d", ter2bin(hep2ter("AJY")));
// decimal to heptavintimal
print_heptavintimal(bin2ter(1024));
incoming: heptavintimal 2023