Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
269 items
•
Updated
•
1
Sign extend a 4-bit signed integer to 8 bits.
Copies the sign bit (MSB) to the upper 4 bits:
| 4-bit | signed | 8-bit | signed |
|---|---|---|---|
| 0000 | 0 | 00000000 | 0 |
| 0001 | 1 | 00000001 | 1 |
| 0111 | 7 | 00000111 | 7 |
| 1000 | -8 | 11111000 | -8 |
| 1111 | -1 | 11111111 | -1 |
Single layer with 8 neurons. Each neuron passes through one input bit:
| Output | Input | Weights | Bias |
|---|---|---|---|
| y0 | x0 | [0,0,0,1] | -1 |
| y1 | x1 | [0,0,1,0] | -1 |
| y2 | x2 | [0,1,0,0] | -1 |
| y3 | x3 | [1,0,0,0] | -1 |
| y4 | x3 | [1,0,0,0] | -1 |
| y5 | x3 | [1,0,0,0] | -1 |
| y6 | x3 | [1,0,0,0] | -1 |
| y7 | x3 | [1,0,0,0] | -1 |
| Inputs | 4 |
| Outputs | 8 |
| Neurons | 8 |
| Layers | 1 |
| Parameters | 40 |
| Magnitude | 16 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def signextend4to8(x3, x2, x1, x0):
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)
for i in range(8)]
# Sign extend -3 (1101 in 4-bit) to 8-bit
result = signextend4to8(1, 1, 0, 1)
print(result) # [1, 0, 1, 1, 1, 1, 1, 1] = 11111101 = -3
MIT