A bar detector
You will learn
Why a neuron's weights work as a template.
Give the neuron the weights of a vertical bar and its sum counts agreements minus disagreements with them. The vertical bar sums to 9 and fires +1, the inverted bar sums to -9 and fires -1, the horizontal bar and the diagonal sum to 1 and noise to 0. The lesson spec is the same neuron in hardware: a combinational BitNet neuron over one 27-trit chunk.
Try it
Edit the input until the sum is exactly the threshold 5; then find the smallest change to the vertical bar that stops it firing.

The weights are a vertical bar. Try every input: the bar fires +1, the inverted bar -1, other shapes and noise stay at 0, with the sums listed.
specs/ternary/comb_bitnet_neuron.t27
module CombBitnetNeuron;
// #1764: a full COMBINATIONAL BitNet neuron in one module = MAC + activation.
//
// `on_comb(a, b)` takes a packed 27-trit weight vector `a` and activation vector
// `b` on input data ports, computes their bit-exact ternary dot product, and
// re-ternarizes the sum to a trit output `result = quantize(dot27(a, b))`. That
// is exactly a BitNet neuron over one 27-trit chunk: weighted sum -> sign.
//
// It is a single self-contained hardware module: (a, b) input ports -> a LUT
// adder-tree (dot27) -> sign comparators (quantize) -> `result` output port,
// generated from spec and synthesizing to Artix-7 LUTs with NO flip-flops
// (purely combinational). Composed with the streaming accumulator
// (stream_ternary_mac.t27) this scales to a multi-chunk neuron; here the whole
// neuron is combinational, so a single (a, b) pair yields the activation in one
// LUT delay.
//
// dot27 / tmul / tp are the bit-exact primitives (#1743); quantize is the
// ternary activation (sign with a zero dead-band).
fn tmul(ta: u8, tb: u8) -> i8 {
if (ta == 1) { return 0; }
if (tb == 1) { return 0; }
if (ta == tb) { return 1; }
return -1;
}
fn tp(a: u64, b: u64, i: u32) -> i8 {
return tmul(((a >> (i << 1)) & 3) as u8, ((b >> (i << 1)) & 3) as u8);
}
fn dot27(a: u64, b: u64) -> i8 {
return tp(a,b,0) + tp(a,b,1) + tp(a,b,2) + tp(a,b,3) + tp(a,b,4)
+ tp(a,b,5) + tp(a,b,6) + tp(a,b,7) + tp(a,b,8) + tp(a,b,9)
+ tp(a,b,10) + tp(a,b,11) + tp(a,b,12) + tp(a,b,13) + tp(a,b,14)
+ tp(a,b,15) + tp(a,b,16) + tp(a,b,17) + tp(a,b,18) + tp(a,b,19)
+ tp(a,b,20) + tp(a,b,21) + tp(a,b,22) + tp(a,b,23) + tp(a,b,24)
+ tp(a,b,25) + tp(a,b,26);
}
// Ternary activation: sign of the weighted sum into a trit {N=0, Z=1, P=2}.
fn quantize(v: i8) -> u8 {
if (v > 0) { return 2; }
if (v < 0) { return 0; }
return 1;
}
// The neuron: weighted sum then sign, exposed as `result` output port.
fn on_comb(a: u64, b: u64) -> u8 {
return quantize(dot27(a, b));
}
test neuron_pp { assert_eq(on_comb(12009599006321322, 12009599006321322), 2); }
test neuron_np { assert_eq(on_comb(0, 12009599006321322), 0); }
test neuron_zz { assert_eq(on_comb(6004799503160661, 6004799503160661), 1); }
test neuron_nn { assert_eq(on_comb(0, 0), 2); }
endmodule
All lessons
Module 1 · Three values
Why three, how balanced ternary writes every number without a sign, and what flipping and cutting trits do.
Module 2 · Logic with unknown
Kleene's three-valued gates, two gates binary has no twin for, and addition as a pair of tables.
Module 3 · Adding trits
The half adder, the full adder and a carry that ripples left, one place at a time.
Module 4 · Multiplying without a multiplier
Copy, drop or flip: a product by one trit, long multiplication, and a MAC that only adds.
Module 5 · Trits in binary memory
Two bits per trit, five trits per byte, and the bits a 27-trit word needs.
Module 6 · The TRI-27 instruction word
The 32-bit word the Trinity emulator decodes: its fields, its 47 opcodes and its 15-bit immediate.
Module 7 · A ternary machine
An ALU built from this course's adders, a three-way jump, and a program you can step.
Module 8 · Three answers
Compare at the highest differing trit, find a number in thirds, sort with three-way compares.
Module 9 · Ternary neurons
Weights of -1, 0, +1: one neuron, a detector and a small layer, with no multiplier anywhere.