Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
4-bit count trailing zeros. Returns the number of consecutive zero bits starting from the LSB.
ctz4(x3, x2, x1, x0) -> count (0-4)
| Input | CTZ | Output |
|---|---|---|
| 0001 | 0 | 000 |
| 0010 | 1 | 001 |
| 0100 | 2 | 010 |
| 1000 | 3 | 011 |
| 0000 | 4 | 100 |
| 0110 | 1 | 001 |
| 1100 | 2 | 010 |
x3 x2 x1 x0
β
βΌ
βββββββββββββββββββββββββββββββββββ
β Layer 1: Prefix conditions β
β p0 = (x0=0) β
β p01 = (x0=x1=0) β
β p012 = (x0=x1=x2=0) β
β all_zero = (all=0) β
βββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββ
β Layer 2: One-hot position β
β z1 = p0 AND x1 β
β z2 = p01 AND x2 β
β z3 = p012 AND x3 β
βββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββ
β Layer 3: Binary encoding β
β y2 = all_zero β
β y1 = z2 OR z3 β
β y0 = z1 OR z3 β
βββββββββββββββββββββββββββββββββββ
β
βΌ
y2 y1 y0
| Inputs | 4 |
| Outputs | 3 |
| Neurons | 10 |
| Layers | 3 |
| Parameters | 37 |
| Magnitude | 30 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
# ctz4(0,1,0,0) = 2 (binary 0100 has 2 trailing zeros)
# See model.py for full implementation
MIT