Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
4-bit two's complement negation circuit. Computes -A for signed integers.
A[3:0] βββΊ Negate βββ¬βββΊ N[3:0] (-A)
ββββΊ overflow (when A = -8)
-A = ~A + 1
1. Invert all bits
2. Add 1
| A (unsigned) | A (signed) | N (signed) | Overflow |
|---|---|---|---|
| 0000 (0) | +0 | +0 | 0 |
| 0001 (1) | +1 | -1 | 0 |
| 0111 (7) | +7 | -7 | 0 |
| 1000 (8) | -8 | -8 | 1 |
| 1111 (15) | -1 | +1 | 0 |
The value -8 (1000) cannot be negated in 4-bit two's complement:
| Component | Count | Neurons |
|---|---|---|
| Bit inverters | 4 | 4 |
| Half-adder chain | 4 | 16 |
| Overflow detect | 1 | 2 |
Total: 22 neurons, 63 parameters, 4 layers
-(+5) = ~(0101) + 1 = 1010 + 1 = 1011 = -5 β
-(-3) = ~(1101) + 1 = 0010 + 1 = 0011 = +3 β
-(-8) = ~(1000) + 1 = 0111 + 1 = 1000 = -8 (overflow!)
from safetensors.torch import load_file
w = load_file('model.safetensors')
# All 16 cases verified
# Overflow only when A = -8 (1000)
threshold-twos-complement/
βββ model.safetensors
βββ create_safetensors.py
βββ config.json
βββ README.md
MIT