Two's complement is a binary number representation in which negative integers are encoded by inverting all bits of the corresponding positive value and adding one. It is the dominant signed-integer encoding used in modern processors because it allows addition, subtraction, and most integer operations to work correctly across both positive and negative operands using the same hardware circuits, with minimal special-casing for sign (though some multiplication and division implementations still handle signed and unsigned variants separately).
In practice
In most embedded processors — including ARM Cortex-M, AVR, MSP430, MIPS-based, and x86 MCUs — signed integer arithmetic uses two's complement encoding. For an N-bit register, the representable range is -2^(N-1) to 2^(N-1) - 1. For example, a signed 8-bit value spans -128 to 127, while an unsigned 8-bit value spans 0 to 255. The asymmetry (one more negative value than positive) is a defining property of the encoding and can surprise developers who assume abs(INT_MIN) is always representable; on an 8-bit type, attempting abs(-128) produces undefined behavior in C, and on typical two's complement hardware the result wraps back to -128, but this is not guaranteed by the language.
Conversion to two's complement by hand: take the positive magnitude in binary, invert every bit (one's complement), then add 1. For example, +5 in 8-bit binary is 0000_0101; inverted gives 1111_1010; adding 1 gives 1111_1011, which is -5. Checking the math: 0000_0101 + 1111_1011 = 0000_0000 with a carry out, confirming the pair sums to zero modulo 2^8.
In C, signed integer overflow is undefined behavior, even though the underlying hardware almost always wraps in two's complement fashion on conventional MCUs. This distinction matters: the compiler is permitted to optimize away checks that rely on overflow wrapping (e.g., `if (x + 1 < x)`), and aggressive optimizers like GCC and Clang do exploit this. Use unsigned types or compiler flags such as `-fwrapv` if you need guaranteed wrap-around semantics. The C99/C11 standards permitted one's complement and sign-magnitude as alternatives (and also allowed padding bits and trap representations in those earlier formulations); C23 mandates two's complement for all signed integers, formalizing decades of de facto practice.
When working with fixed-point arithmetic, ADC results, or raw register values in embedded code, it is common to receive a two's complement value packed into a wider register or a byte array. Correctly sign-extending a 12-bit ADC result stored in a 16-bit variable, or a 24-bit signed sensor word split across three bytes, requires deliberate bit manipulation. A programmer's calculator that displays hex, binary, and decimal simultaneously — such as the one discussed in the "Embedded Toolbox: Programmer's Calculator" blog post on EmbeddedRelated — is invaluable for verifying these conversions by hand.
Frequently asked
Why does two's complement have one more negative value than positive?
With N bits there are 2^N patterns. Two's complement assigns one pattern to zero, leaving an odd number (2^N - 1) to split between positives and negatives. Zero shares no pattern with a negative counterpart, so the negatives get one extra slot. The result is that INT_MIN (e.g., -128 for 8-bit) has no positive equivalent within the same type — attempting to negate it or call abs() on it produces undefined behavior in C, and on typical two's complement hardware wraps back to INT_MIN.
Why is two's complement preferred over sign-magnitude or one's complement?
Sign-magnitude and one's complement both have two representations of zero (positive zero and negative zero), which complicates comparators and adder hardware. Two's complement has a unique zero and allows a single adder circuit to handle both signed and unsigned addition correctly. Subtraction is implemented as addition of the two's complement negation, further simplifying the ALU.
Is signed integer overflow in C safe to rely on for two's complement wrapping?
No. In C prior to C23, signed integer overflow is undefined behavior regardless of the hardware representation. Compilers targeting two's complement hardware are still permitted — and in practice do — optimize code in ways that assume overflow never occurs. If wrap-around arithmetic is needed, use unsigned types (which are guaranteed to wrap modulo 2^N in all C standards), cast carefully, or compile with -fwrapv to force defined wrapping semantics.
How do I sign-extend a small two's complement value into a wider type?
If the value is already in a signed type of the correct width, assigning it to a wider signed type will preserve its value on virtually all C implementations in practice, though the exact mechanism (sign extension) is an implementation detail rather than an explicit language guarantee. If the value is packed into the lower bits of an unsigned variable — for example, a 12-bit
ADC result in a uint16_t — one common approach is to left-shift it to the MSB of a signed type and then arithmetic-right-shift back: `int16_t result = (int16_t)(raw << 4) >> 4;`. Be aware that right-shifting signed values is implementation-defined in C, though it behaves as arithmetic shift on the vast majority of embedded compilers and targets.
Does two's complement apply to floating-point numbers?
No.
Floating-point formats (
IEEE 754 single and double precision, used on MCUs and processors that include an
FPU, such as Cortex-M4/M7 — though many microcontrollers lack an FPU entirely or support only optional extensions) use a separate sign bit combined with a biased exponent and a significand mantissa. Two's complement is exclusively a fixed-width integer encoding.
Differentiators vs similar concepts
Two's complement is frequently contrasted with sign-magnitude and one's complement, the two other historical signed-integer encodings. Sign-magnitude uses the MSB purely as a sign flag and the remaining bits as the unsigned magnitude; it has two zeros (0000_0000 and 1000_0000 for 8-bit) and requires separate adder logic for signed operations. One's complement negates by bit-inversion alone, which also produces two zeros and requires an end-around carry in the adder. Two's complement eliminates both problems. It should not be confused with offset binary (also called excess-N or biased representation), which is sometimes used for
ADC output and
floating-point exponents, where the all-zeros pattern represents the most negative value rather than zero.