Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
Subtracts two 1-bit inputs, producing difference and borrow. The subtraction counterpart to the half adder.
a b a b
β β β β
βββββ¬ββββ€ βββ¬ββ
β β β β
βΌ β βΌ βΌ
βββββββββββββββββ βββββββββ
β OR βββ NAND β β Β¬a β§ bβ
βw:1,1 βββw:-1,-1β βw:-1,1 β
βb: -1 βββb: +1 β βb: -1 β
βββββββββββββββββ βββββββββ
β β β β
βββββΌββββ β
βΌ β
ββββββββ β
β AND β β
βw: 1,1β β
βb: -2 β β
ββββββββ β
β β
βΌ βΌ
diff borrow
The diff output uses XOR (2 layers). The borrow output detects when we subtract 1 from 0.
| a | b | diff | borrow |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
Binary: a - b = diff - (borrow Γ 2)
Diff (XOR): Same as the half adder's sum. The difference bit is 1 when exactly one input is 1.
Borrow: Uses weights [-1, +1] with bias -1. This fires only when a=0 and b=1:
The borrow is NOT(a) AND b - we borrow when subtracting 1 from 0.
| Output | Function | Neurons | Layers |
|---|---|---|---|
| diff | XOR(a, b) | 3 | 2 |
| borrow | Β¬a β§ b | 1 | 1 |
Total: 4 neurons, 12 parameters
| Circuit | Output 2 | Function | Weights |
|---|---|---|---|
| HalfAdder | carry | a β§ b | [1, 1], b=-2 |
| HalfSubtractor | borrow | Β¬a β§ b | [-1, 1], b=-1 |
The only difference: borrow has an inhibitory weight on a.
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def half_subtractor(a, b):
inp = torch.tensor([float(a), float(b)])
# XOR for diff
l1 = (inp @ w['xor.layer1.weight'].T + w['xor.layer1.bias'] >= 0).float()
diff = int((l1 @ w['xor.layer2.weight'].T + w['xor.layer2.bias'] >= 0).item())
# Borrow
borrow = int((inp @ w['borrow.weight'].T + w['borrow.bias'] >= 0).item())
return diff, borrow
threshold-halfsubtractor/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
MIT