wire / wire-format / twos-complement
Two's complement is the standard representation for signed integers. An n-bit two's complement number: the most significant bit is the sign bit (0 = non-negative, 1 = negative). Negative value of x = (2^n - x) = invert all bits of x and add 1. Range: -2^(n-1) to 2^(n-1)-1. Addition and subtraction use the same hardware as unsigned (overflow wraps).
aka: Two's complement (signed integer representation)
spec: Two's complement (standard)
Two's complement is the standard representation for signed integers. An n-bit two's complement number: the most significant bit is the sign bit (0 = non-negative, 1 = negative). Negative value of x = (2^n - x) = invert all bits of x and add 1. Range: -2^(n-1) to 2^(n-1)-1. Addition and subtraction use the same hardware as unsigned (overflow wraps).
| field | size | meaning |
|---|---|---|
| Sign bit | 1 bit (MSB) | 0 = non-negative, 1 = negative. |
| Magnitude | n-1 bits | For non-negative: the value. For negative: 2^n - |value|. |
| Negation | n bits | Negate x: ~x + 1 (invert all bits, add 1). Or: 2^n - x. |
| Range (n-bit) | n bits | -2^(n-1) to 2^(n-1)-1. E.g., 8-bit: -128 to 127. 32-bit: -2^31 to 2^31-1. 64-bit: -2^63 to 2^63-1. |
| Zero | n bits | Only one representation of zero (all zeros). (Unlike sign-magnitude which has +0 and -0.) |
| Overflow | n bits | Overflow occurs when the result exceeds the range. Detected by: sign of operands same, sign of result different. |
example:
ff ff ff ff
32-bit two's complement: -1 = 0xFFFFFFFF
agent: curl -H 'accept: application/json' wire.phall.io/wire-format/twos-complement
or /wire-format/twos-complement.json