--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - arithmetic - bcd --- # threshold-bcd-adder BCD (Binary-Coded Decimal) single-digit adder as threshold circuit. Adds two decimal digits (0-9) with carry. ## Circuit ``` A[3:0] ──┐ (BCD digit 0-9) B[3:0] ──┼──► BCD Adder ──┬──► S[3:0] (BCD digit 0-9) Cin ──┘ └──► Cout (decimal carry) ``` ## Algorithm ``` 1. Binary add: Z = A + B + Cin (4-bit result + carry) 2. Detect overflow: Z > 9 OR binary carry 3. If overflow: add 6 (0110) for correction 4. Decimal carry = overflow detected ``` ## Why Add 6? BCD uses only values 0-9. When binary sum exceeds 9: - 10 (1010) + 6 = 16 (10000) → 0 with carry - 11 (1011) + 6 = 17 (10001) → 1 with carry - etc. Adding 6 skips the invalid codes A-F (10-15). ## Truth Table (examples) | A | B | Cin | S | Cout | |---|---|-----|---|------| | 5 | 3 | 0 | 8 | 0 | | 5 | 5 | 0 | 0 | 1 | | 9 | 9 | 0 | 8 | 1 | | 9 | 9 | 1 | 9 | 1 | ## Architecture | Stage | Component | Neurons | |-------|-----------|---------| | Binary add | 4 Full Adders | 28 | | Detect >9 | AND, OR gates | 4 | | Correction | 2 Half Adders + XOR | 11 | **Total: 43 neurons, 133 parameters, 5 layers** ## Applications - Decimal arithmetic in calculators - Financial calculations requiring exact decimal - BCD-based displays (7-segment) - Legacy systems using packed BCD ## Usage ```python from safetensors.torch import load_file w = load_file('model.safetensors') # Valid inputs: A, B in range 0-9 # All 200 test cases verified (10 × 10 × 2) ``` ## Files ``` threshold-bcd-adder/ ├── model.safetensors ├── create_safetensors.py ├── config.json └── README.md ``` ## License MIT