next up previous
Next: Multiplication and Division Up: Integer Representation and Arithmetic Previous: Carry Lookahead Summary

Signed Integers

When we extend our binary representations to include negative numbers, there are several choices. For example, the sign and magnitude approach is to have one bit for the sign and $n-1$ bits for the magnitude. The one's complement approach is to represent negative numbers by having a bit for sign and $n-1$ bits for the magnitude when unsigned and to represent negation by flipping all bits. Among other things, these representations have two representations for zero, one positive and one negative. Nowadays, most computers use two's complement which has the useful property of being able to use the same addition circuits that we have discussed for unsigned integers.

In two's complement, an non-negative number is represented by having a zero in the high order bit (still called the sign bit) and $n-1$ bits in the normal unsigned format. This represents the numbers from $0
\ldots 2^{n-1}-1$. The negation of a number $x$ is chosen such that $x
+ -x = 0$.

To negate a number, one may invert all the bits and increment the result. The sum of a number and the result of all its bits is $2^n-1$ so one more than inverting all the bits gives zero because there are only $n$ bits. Note that this negation operation does not change zero. After using the negation operation to define more values, there is one combinations of bits which is undefined, a single bit set to one followed by zeros. We define this to be $-2^{n-1}$.

As with our unsigned integer representation, there is a simple formula for the value of a signed integer given its two's complement representation.

\begin{eqnarray*}
\mathit{value}(b_{n-1} b_{n-2} \ldots b_1 b_0) & = & b_{n-1} *
-2^{n-1} + b_{n-2} * 2^{n-2} + \ldots + b_1 * 2^1 + b_0 * 2^0 \\
\end{eqnarray*}



This is equivalent to our unsigned representation modulo $2^n$. You should convince yourself that the sign bit is set correctly when adding two numbers unless there is overflow. Particularly, when adding two negative numbers, the two sign bits set to one effectively cancel each other out but there will be a carry bit from the lower $n-1$ bits to set it unless the result would be less than $-2^{n-1}$.

Changing the size of a signed integer is slightly more complicated than with unsigned integers. When increasing the size, the old sign bit is preserved and copied to the new sign bit. Note that $b_{n-1} *
-2^{n-1} = b_{n-1} * (-2^n + 2^{n-1}) = b_{n-1} * -2^n + b_{n-1} *
2^{n-1}$. This is called sign extension. Similarly, when decreasing the size, the value remains the same as long as the two high order bits are the same.

Since every combination of bits in two's complement represents a different integer, we can use the same equality circuits as for unsigned integers. When comparing two signed integers in two's complement, we can use almost the same hardware, except that the usage of the highest order bit needs to be reverse because it corresponds to a negative value now.


next up previous
Next: Multiplication and Division Up: Integer Representation and Arithmetic Previous: Carry Lookahead Summary
Jeffrey Considine 2001-05-01