Computers: Numbers: Representing Signed Integers
Positive binary integers have a very natural representation.
Several systems have been used to represent negative integers.
Some of the most common are listed below.
The even number of bit patterns in a given amount of memory (eg, the 32 bits
that are typically allocated for an integer)
has some interesting consequences for representing signed numbers.
- Symmetric range. If the range is symmetric (the minimum number is equal to the negative
of the maximum number as in signed-magnitude and one's-complement),
then there are two zeros (+0 and -0)!
- Single zero. If there is only one zero (as in two's complement and excess-notation),
then the range has one more negative than positive number.
- Illegal value. I don't know of a system that uses one particular bit pattern to represent
an illegal integer value, but I'm sure it's been proposed. Too bad it isn't
in common use.
Negative integers
- Two's complement is the system used on all modern systems.
Change from positive to negative or vice versa by inverting all bits
(change 1's to 0's and vice versa)
and adding one. There is only one zero, but the range is asymmetric (-128..+127).
The first bit is effectively a sign bit.
- Signed-magnitude uses the first bit to represent a sign (0 is positive).
The range is symmetric, but there are two zeros (one positive and one negative).
Used in most early computer architectures.
- One's complement simply inverts all the bits. The range is symmetric,
but there are two zeros. The first bit is effectively a sign bit.
Used in some early computer architectures.
- Excess notation uses the full unsigned range and assigns zero to some
arbitrary position. The amount that zero is shifted up is called the bias.
This notation is used for the exponent field of IEEE-754 floating-point.
In single-precision floating point, the 8-bit exponent field's bias is 127,
which is used in the example below. Excess notation is also used for
other specialized data representations, but isn't used for general integers.
Sample values
Here are some sample values of 8-bit integer representations.
| Signed Magnitude | One's Complement | Two's Complement | Excess bias 127 | Unsigned |
| -128 | none | none | 1000 0000 | none | none |
| -127 | 1111 1111 | 1000 0000 | 1000 0001 | 0000 0000 | none |
| -2 | 1000 0010 | 1111 1101 | 1111 1110 | 0111 1101 | none |
| -1 | 1000 0001 | 1111 1110 | 1111 1111 | 0111 1110 | none |
| 0 | 0000 0000 1000 0000 | 0000 0000 1111 1111 | 0000 0000 | 0111 1111 | 0000 0000 |
| +1 | 0000 0001 | 0000 0001 | 0000 0001 | 1000 0000 | 0000 0001 |
| +2 | 0000 0010 | 0000 0010 | 0000 0010 | 1000 0001 | 0000 0010 |
| +126 | 0111 1110 | 0111 1110 | 0111 1110 | 1111 1101 | 0111 1110 |
| +127 | 0111 1111 | 0111 1111 | 0111 1111 | 1111 1110 | 0111 1111 |
| +128 | none | none | none | 1111 1111 | 1000 0000 |
| +129 | none | none | none | none | 1000 0001 |
| +255 | none | none | none | none | 1111 1111 |
And the winner is: Two's complement
The most compelling factors for microprocessor manufacturers
are speed (maximize),
integrated circuit "real estate" (minimize), and power requirements (minimize). Two's complement must have
advantages over other systems because all new CPUs use it.