Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks. • 269 items • Updated • 1
Adds two 1-bit inputs, producing sum and carry. The building block for all multi-bit adders.
a b
│ │
├───┬───┤
│ │ │
▼ │ ▼
┌──────┐│┌──────┐
│ OR │││ NAND │
└──────┘│└──────┘
│ │ │
└───┼───┘
▼ a b
┌──────┐ │ │
│ AND │ └─┬─┘
└──────┘ ▼
│ ┌──────┐
▼ │ AND │
sum └──────┘
│
▼
carry
The sum output uses XOR (2 layers), the carry uses AND (1 layer).
| a | b | sum | carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
Binary: a + b = (carry × 2) + sum
| Output | Function | Neurons | Layers |
|---|---|---|---|
| sum | XOR(a, b) | 3 | 2 |
| carry | AND(a, b) | 1 | 1 |
Total: 4 neurons, 12 parameters
The half adder computes a + b where a, b ∈ {0, 1}:
The carry represents the 2s place.
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def half_adder(a, b):
inp = torch.tensor([float(a), float(b)])
# XOR for sum
or_out = int((inp @ w['xor.layer1.or.weight'] + w['xor.layer1.or.bias']).sum() >= 0)
nand_out = int((inp @ w['xor.layer1.nand.weight'] + w['xor.layer1.nand.bias']).sum() >= 0)
xor_inp = torch.tensor([float(or_out), float(nand_out)])
sum_out = int((xor_inp @ w['xor.layer2.weight'] + w['xor.layer2.bias']).sum() >= 0)
# AND for carry
carry_out = int((inp @ w['carry.weight'] + w['carry.bias']).sum() >= 0)
return sum_out, carry_out
threshold-halfadder/
├── model.safetensors
├── model.py
├── config.json
└── README.md
MIT
Totally Free + Zero Barriers + No Login Required