Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
248 items
•
Updated
•
1
4-bit find first set (priority encoder). Returns 1-indexed position of the least significant set bit.
ffs4(a3, a2, a1, a0) = position of first (least significant) 1 bit, 0 if no bits set
| Input | First bit | Output (y2,y1,y0) |
|---|---|---|
| 0000 | none | 000 (0) |
| 0001 | bit 0 | 001 (1) |
| 0010 | bit 1 | 010 (2) |
| 0011 | bit 0 | 001 (1) |
| 0100 | bit 2 | 011 (3) |
| 1000 | bit 3 | 100 (4) |
| 1010 | bit 1 | 010 (2) |
Layer 1: Priority detection
has0 = a0 (bit 0 is set)
has1_first = a1 AND NOT(a0)
has2_first = a2 AND NOT(a1) AND NOT(a0)
has3_first = a3 AND NOT(a2) AND NOT(a1) AND NOT(a0)
Layer 2: Binary encoding
y0 = has0 OR has2_first (ffs is 1 or 3)
y1 = has1_first OR has2_first (ffs is 2 or 3)
y2 = has3_first (ffs is 4)
| Inputs | 4 |
| Outputs | 3 |
| Neurons | 7 |
| Layers | 2 |
| Parameters | 35 |
| Magnitude | 22 |
from safetensors.torch import load_file
# See model.py for full implementation
# ffs4(1, 0, 1, 0) = [0, 1, 0] = 2 (bit 1 is first set)
# ffs4(0, 0, 0, 1) = [0, 0, 1] = 1 (bit 0 is first set)
MIT