wire / wire-format / twos-complement

Two's complement (signed integer representation)

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).

wire-format kind wire-format status standard verification verified tier B wire-format@1

aka: Two's complement (signed integer representation)

wire format

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).

fieldsizemeaning
Sign bit1 bit (MSB)0 = non-negative, 1 = negative.
Magnituden-1 bitsFor non-negative: the value. For negative: 2^n - |value|.
Negationn bitsNegate 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.
Zeron bitsOnly one representation of zero (all zeros). (Unlike sign-magnitude which has +0 and -0.)
Overflown bitsOverflow 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


provenance

agent: curl -H 'accept: application/json' wire.phall.io/wire-format/twos-complement or /wire-format/twos-complement.json