- The 2's complement number system is a common number system used for expressing signed numbers in binary. In this system, the most significant bit acquires a negative value. Thus, if the most significant bit is a "1", the entire number is negative. All other digits represent positive numbers. For example, if we have the bitstring:
We can determine the (decimal) value as follows:
- The most significant bit is a 1, so it is negative. It is in the 7th place, so it has an absolute value of 27. Thus, the most significant bit contributes -27 = -128.
- The next three bits are zero, so they contribute nothing to the value.
- The last four bits represent the number 15. Since they are not in the most significant bit's place, 15 is positive.
- We add up all the numbers to get: -128+15 = -113.
Note that we can only represent numbers from -128 to 127.
Or, in general, for a bitstring of length x bits we can represent numbers from -2x-1 up to 2x-1-1.
Or "to take the 2's complement", meaning to take the opposite of a 2's complement number. Using our example above, we perform the following procedure:
- Take the 1's complement or invert each bit => We get: 0111 0000.
- Add 1 => We get 0111 0001
Determining the decimal value as above, we see that the resulting number is indeed +113.
Other things to see: infinity, unsigned.