File size: 1,780 Bytes
49eaae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
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