Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
269 items
β’
Updated
β’
1
Adds two 4-bit numbers with carry-in. Four cascaded full adders in the classic ripple-carry architecture.
a0 b0 cin a1 b1 a2 b2 a3 b3
β β β β β β β β β
ββββΌβββ ββββΌβββ ββββΌβββ ββββΌβββ
βΌ βΌ βΌ βΌ
βββββββββ βββββββββ βββββββββ βββββββββ
β FA0 ββββc0ββββ FA1 βββc1βββ FA2 βββc2βββ FA3 ββββcout
βββββββββ βββββββββ βββββββββ βββββββββ
β β β β
βΌ βΌ βΌ βΌ
s0 s1 s2 s3
Input: (a3 a2 a1 a0) + (b3 b2 b1 b0) + cin
Output: (cout s3 s2 s1 s0)
The carry ripples through each full adder sequentially, hence "ripple-carry."
a b
β β
βββββ¬ββββ
βΌ
βββββββββββ
β HA1 β Half adder 1
βββββββββββ
β β
s1 c1
β \
β cin \
ββββ¬βββ \
βΌ \
βββββββββββ \
β HA2 β β
βββββββββββ β
β β β
sum c2 β
β β
ββββ¬ββββ
βΌ
ββββββββ
β OR β
ββββββββ
β
βΌ
cout
1111 (15)
+ 0001 ( 1)
ββββββ
10000 (16)
15 + 1 = 16, which overflows 4 bits. The result is s=[0,0,0,0] with cout=1.
| Component | Per FA | Total (4 FAs) |
|---|---|---|
| Neurons | 9 | 36 |
| Parameters | 27 | 108 |
| Layers | 4 | 16 |
| Name | Bits | Description |
|---|---|---|
| a | a3..a0 | First 4-bit number (LSB = a0) |
| b | b3..b0 | Second 4-bit number (LSB = b0) |
| cin | 1 | Carry in |
| s | s3..s0 | 4-bit sum (LSB = s0) |
| cout | 1 | Carry out |
The worst-case delay is when a carry must propagate through all 4 full adders:
cin β FA0.cout β FA1.cout β FA2.cout β FA3.cout
This is 4 Γ 4 = 16 layers deep. Carry-lookahead would reduce this but requires more complex circuitry.
from safetensors.torch import load_file
w = load_file('model.safetensors')
def ripple_carry_4bit(a, b, cin):
"""a, b: 4-bit lists [a0,a1,a2,a3] (LSB first)"""
# See model.py for full implementation
pass
# Example: 7 + 5 = 12
a = [1, 1, 1, 0] # 7 in LSB-first
b = [1, 0, 1, 0] # 5 in LSB-first
sums, cout = ripple_carry_4bit(a, b, 0)
# sums = [0, 0, 1, 1], cout = 0 β 12
| Bits | Full Adders | Neurons | Parameters | Max Depth |
|---|---|---|---|---|
| 2 | 2 | 18 | 54 | 8 |
| 4 | 4 | 36 | 108 | 16 |
| 8 | 8 | 72 | 216 | 32 |
| n | n | 9n | 27n | 4n |
threshold-ripplecarry4bit/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
MIT