XXIIVV

-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 valueUnsignedBalancedBCB
false0-11
unknown1000
truth2+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.

UnsignedBCBBalanced
Dec.Tern.Tern.Dec.
0001111---4
1011100-0-3
2021101-+-2
31000110--1
4110000000
51200010+1
6200111+-2
7210100+03
8220101++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+
bc1110001
int-101
Balanced Tribble
try---000+++
bc3111111000000010101
int-1300013

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