--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - arithmetic - signed --- # threshold-twos-complement 4-bit two's complement negation circuit. Computes -A for signed integers. ## Circuit ``` A[3:0] ──► Negate ──┬──► N[3:0] (-A) └──► overflow (when A = -8) ``` ## Algorithm ``` -A = ~A + 1 1. Invert all bits 2. Add 1 ``` ## Truth Table | 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 | ## Overflow The value -8 (1000) cannot be negated in 4-bit two's complement: - Range: -8 to +7 - -(-8) = +8, which exceeds +7 - Result wraps to -8, overflow flag set ## Architecture | Component | Count | Neurons | |-----------|-------|---------| | Bit inverters | 4 | 4 | | Half-adder chain | 4 | 16 | | Overflow detect | 1 | 2 | **Total: 22 neurons, 63 parameters, 4 layers** ## Examples ``` -(+5) = ~(0101) + 1 = 1010 + 1 = 1011 = -5 ✓ -(-3) = ~(1101) + 1 = 0010 + 1 = 0011 = +3 ✓ -(-8) = ~(1000) + 1 = 0111 + 1 = 1000 = -8 (overflow!) ``` ## Usage ```python from safetensors.torch import load_file w = load_file('model.safetensors') # All 16 cases verified # Overflow only when A = -8 (1000) ``` ## Files ``` threshold-twos-complement/ ├── model.safetensors ├── create_safetensors.py ├── config.json └── README.md ``` ## License MIT