Two’s complement

Since computers only use 0 and 1 to represent numbers, the question arose how to represent negative integer numbers. The basic idea here is to invert all bits of a positive integer to get the corresponding negative integer (this would be the one’s complement). But this method has the slight drawback that zero is represented twice (all bits 0 and all bits 1). Therefore, a better way is to represent negative numbers by inverting the positive number and adding 1. For +1 and a 4-bit representation, this leads to:

1 = 0001 → −1 = 1110 + 1 = 1111.

For zero, we obtain

0 = 0000 → −0 = 1111 + 1 = 0000,

so there is only one representation for zero now. This method of representation is called the two’s complement and is used in microcontrollers. With n bits it represents values within [−2 n−1 , 2 n−1 − 1].