All courses › Embedded Systems and Instrumentation › Two's Complement

Two's Complement

Two's complement is the standard way computers represent negative integers in a fixed number of bits. The top bit signals the sign, and a negative number −x-x is represented as the unsigned bit pattern of 2n−x2^n-x. This makes addition work the same way for positive and negative numbers, with no special cases.

−x→2n−x-x \to 2^n-xbit pattern for a negative number in n-bit two's complement
[−2n−1, 2n−1−1][-2^{n-1},\ 2^{n-1}-1]range of an n-bit signed integer

Symbols

nnnumber of bits
xxthe number's absolute value

Example

−5-5 as 8-bit two's complement:

28−5=251=0xFB2^8-5=251=\texttt{0xFB}.

Remember to subtract from 2n2^n, not just flip the sign bit – that gives the wrong answer.
Practise number systems and digital logic for free →

← Number Systems · Logic Gates →

Part of Embedded Systems and Instrumentation: Number systems and digital logic.