next up previous
Next: Fixed Point?-Integral in Our Up: numbers Previous: Sign(ed) Magnitude

Sign(ed) 2's Complement

Signed 2's complement (or sign 2's complement) (s2c) is a modification of the sign-magnitude form in which addition and subtraction work the way that you expect them to. The price we pay is that we can't read a negative number directly. The high order bit is still the sign bit, and a ``1'' still indicates a negative number. Furthermore, zero is represented by all zeros and, for this course, a ``1'' followed by all zeros is still an illegal value for us.

The relationship between a number and its s2c equivalent is formalized in the following, and explained below.


\begin{displaymath}(b_k b_{k-1} b_{k-2}\ldots b_2 b_1 b_0)_2 = ((-1)^{b_k}( b_k
+\sum_{i=0}^{k-1} ( b_k \oplus b_i) 2^i)_{10}\end{displaymath}

The s2c form of a positive integer is identical to the sign-magnitude form of the positive integer. So, our ``example'' number 2087 is still given by 13 bits: twelve to capture the magnitude of the number, and one, the msb, set to 0, to signify that the number is positive.


\begin{displaymath}(0\;\; 1000\;\;0010\;\; 0101)_2 =(-1)^0\;\; (1 \times 2^{11} + 1 \times 2^5 + 1 \times 2^2
+ 1 \times 2^0)_{10} \end{displaymath}

In order to encode or determine the s2c form of a negative integer, we first write down the s2c form of its positive form. Then, we bit-wise complement all bits of that number (the zeros become ones, the ones become zeros). This is called forming the one's complement. Once we've done that, we form the 2's complement by adding 1 to the bit string that we have. The result is the s2c form.

So, for -2087, we start with

\begin{displaymath}+2087_{10}=(0\;\; 1000\;\;0010\;\; 0101)_2\end{displaymath}

We complement all the bits, yielding the intermediate expression

\begin{displaymath}(1\;\; 0111\;\;1101\;\; 1010)\end{displaymath}

Then, we add 1, to get


\begin{displaymath}-2087_{10}=(1\;\; 0111\;\;1101\;\; 1011)\end{displaymath}

To ``decode'' a number in s2c form, first check the sign bit. If the sign bit is zero, the conversion is identical to that shown for sign-magnitude positive numbers above. If the sign bit is one, then we know the number is negative. Now that we know the sign, we must decode to get the magnitude. This is done by complementing all the bits and adding one. The magnitude of the resulting number will be that of the negative number you started with.

For example, consider the following 13-bit number in s2c form:


\begin{displaymath}1 1111 1111 1110 \end{displaymath}

Because the msb is ``1'', we know that the number is negative. It remains to determine the magnitude of the number. First, we take the complement of all the bits, referred to as computing the 1's complement.


\begin{displaymath}0 0000 0000 0001 \end{displaymath}

Next, we add ``1'' to the above value to get:


\begin{displaymath}0 0000 0000 0010 \end{displaymath}

Since the above value is positive 2 in binary, the value we started with, all ones except for the least significant bit, must be the s2c form of -2.

Note: Suppose you are asked to compute a s2c equivalent of either positive or negative 2087 above, but the representation must be longer than 13 bits, say 16. All you have to do is extend the sign bit! If the number is positive, fill to the new msb with zeros. If the number is negative, fill to the new msb with ones. This sign extension works for any s2c value.


next up previous
Next: Fixed Point?-Integral in Our Up: numbers Previous: Sign(ed) Magnitude
MM Hugue 2004-09-08