--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic --- # threshold-signbit8 Extract sign bit (MSB) of 8-bit number. ## Function signbit8(a7..a0) = a7 In 2's complement representation: - Output 0: non-negative (0 to 127) - Output 1: negative (-128 to -1) ## Truth Table (selected) | unsigned | signed | signbit | |---------:|-------:|:-------:| | 0 | 0 | 0 | | 127 | +127 | 0 | | 128 | -128 | 1 | | 255 | -1 | 1 | ## Architecture Single neuron extracting MSB: - Weights: [1, 0, 0, 0, 0, 0, 0, 0] - Bias: -1 Fires when: a7 - 1 >= 0, i.e., a7 >= 1 ## Parameters | | | |---|---| | Inputs | 8 | | Outputs | 1 | | Neurons | 1 | | Layers | 1 | | Parameters | 9 | | Magnitude | 2 | ## Usage ```python from safetensors.torch import load_file import torch w = load_file('model.safetensors') def signbit8(a7, a6, a5, a4, a3, a2, a1, a0): inp = torch.tensor([float(a7), float(a6), float(a5), float(a4), float(a3), float(a2), float(a1), float(a0)]) return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item()) print(signbit8(0, 1, 1, 1, 1, 1, 1, 1)) # 0 (value = +127) print(signbit8(1, 0, 0, 0, 0, 0, 0, 0)) # 1 (value = -128) ``` ## License MIT