Binary Signed Integers – Signed Magnitude Shortcomings

Binary NumbersI previously discussed the signed magnitude solution to representing signed integers as binary strings and pointed out that while it had the advantage of being simple, it also has some disadvantages. For starters, N-bit signed magnitude integers have two representations for zero: positive zero (a bitstring with N zeros) and negative zero (a bitstring with a one followed by N-1 zeros).

There is another significant disadvantage that isn’t obvious until you try to implement signed magnitude representation in silicon. Specifically, you can’t do mathematics with signed magnitude integers using the same hardware as is used for unsigned integers.

Let’s consider addition using simple, 4-bit signed magnitude integers. Consider adding 1 + 1:

\begin{tabular}{ccccc} & 0 & 0 & 0 & 1 \\ + & 0 & 0 & 0 & 1 \\ \hline & 0 & 0 & 1 & 0 \\ \end{tabular}

One plus one equals two. So far, so good. What happens if we try -1 + (-1)?

\begin{tabular}{cccccc} & & 1 & 0 & 0 & 1 \\ + & & 1 & 0 & 0 & 1 \\ \hline &1 & 0 & 0 & 1 & 0 \\ \end{tabular}

Since we are adding four bit integers, we discard the overflow bit, and we are left with 0010, or +2, again. We got the magnitude of the sum correct but not the sign. A naive solution would be to ignore the sign bit during the calculation. In which case, -1 + (-1) is:

\begin{tabular}{ccccc} & 1 & 0 & 0 & 1 \\ + & 1 & 0 & 0 & 1 \\ \hline & 1 & 0 & 1 & 0 \\ \end{tabular}

Looks great. -2 is what we expected. While this also works when adding two positive integers, we see it’s not a viable solution when adding a positive and negative integer. Take 1 + (-1):

\begin{tabular}{ccccc} & 0 & 0 & 0 & 1 \\ + & 1 & 0 & 0 & 1 \\ \hline & ? & 0 & 1 & 0 \\ \end{tabular}

Which sign bit do we preserve? Actually it doesn’t matter, because the magnitude of the sum is 010, or 2, which is incorrect whether the sign is positive or negative. Our answer should have been zero. Why is the magnitude of the answer incorrect?

The problem lies with the representation. While signed magnitude does a perfectly acceptable job of representing both the sign and the magnitude of positive and negative integers, it fails to encode an important mathematical relationship between an integer and its negated value. Negating a number should yield the additive inverse of that number, ie., a number plus its negation should yield zero. So while 1001 may represent -1, it does not represent the negation, or additive inverse, of 1 when represented using signed magnitude as 0001.

This does not mean that it is impossible to design hardware and algorithms to handle signed magnitude addition (and other mathematical operations), but it is not convenient given that there are other representations for which the aformentioned issue is not a problem. Ones compliment is one such representation, and I hope to write about it soon. Stay posted.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.