When people write a negative number, they usually write a minus sign first, followed by the number's magnitude. The magnitude is the absolute value of the number. The preceding minus sign tells us it is a negative number. If the minus sign is missing, then we know the number is non-negative. (We do not say it must be positive, because it could be zero. Zero is neither positive nor negative. Zero is not written with a minus sign.)
Harken back to when we were talking about a single bit being able to represent a "TRUE" or "FALSE" piece of information (such as whether a customer did or didn't want raisin's in their bread pudding). We can do the same trick here. Let's use a bit to represent whether a number is negative or non-negative.We'll sacrifice one of the bits in our bit field to be a sign bit. If the sign bit is '1' then the number is negative. If the sign bit is '0' then the number is non-negative. The remaining bits will tell us the magnitude of the number.
Which bit should we sacrifice to be the sign bit? Well, since people write the minus sign to the left of the number, we should probably sacrifice the most significant (leftmost) bit.
So let's try out this new representation for negative binary numbers using a 4-bit field. Here it is:
How would we represent, say, a -3? Well, since there is a minus sign, the sign bit would be set to "TRUE" or '1'. The magnitude is '3', which in binary is '11B'. So, the final bit pattern for representing -3 would be:
What about a positive 6? Well, there's no minus sign, so the sign bit is '0'. The magnitude bits would be '110', and we would have:
What is the most negative number we can represent? It would have the minus bit set to '1' and the large possible magnitude, which is '111B'. So, -7 is the most negative number we can represent with a 4-bit field. What about the most postive? That would a positive 7.
In general, when you use this representation with an n-bit field, the range of numbers that can be represented is:
So far everything looks pretty good, but closer inspection shows some serious problems with this representation, which we call signed magnitude representation. Here is a number line which shows all the numbers that can be represented with a 4-bit signed magnitude number.
Problem #1 with signed magnitude - "The Case of the Missing Bit Pattern" : How many possible bit patterns can be created with 4 bits? Easy, we know that's 24, or 16. In unsigned representation, we were able to represent 16 numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15. But with signed magnitude, we are only able to represent 15 numbers: -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, and 7. There's still 16 bit patterns, but one of them is either not being used or is duplicating a number. That bit pattern is '1000B'. When we interpret this pattern, we get '-0' which is both nonsensical (negative zero? come on!) and redundant (we already have '0000B' to represent 0).
Problem #2 with signed magnitude - "Requires Special Care and Feeding": Remember we wanted to have negative binary numbers so we could use our binary addition algorithm to simulate binary subtraction. How does signed magnitude fare with addition? To test it, let's try subtracting 2 from 5 by adding 5 and -2. A positive 5 would be represented with the bit pattern '0101B' and -2 with '1010B'. Let's add these two numbers and see what the result is:
Now we interpret the result as a signed magnitude number. The sign is '0' (non-negative) and the magnitude is '7'. So the answer is a postive 7. But, wait a minute, 5-2=3! This obviously didn't work. Conclusion: signed magnitude doesn't work with regular binary addition algorithms.
Signed magnitude has more disadvantages than it does advantages.
DISADVANTAGES of signed magnitude: One of the bit patterns is wasted. Addition doesn't work the way we want it to.
First, let's draw a number line which shows positive and negative integers about 0. Again, we will limit ourselves (for right now) to a 4-bit field. Let's keep the bit patterns that represent 0 through 7 since they all meet our requirements (sign bit is easy to test and addition works). Now observe the bit pattern for '0' which is '0000B'. We need a bit pattern to represent -1. In order for this bit pattern to work with addition, then when we add '0001B' to it, we should get '0000B'. What bit pattern, when '0001B' is added to it yields '0000B'?
The answer to this question may not be immediately obvious, until you remember that we have limited ourselves to a 4-bit field. Consider the bit pattern '1111B'. Add it to '0001B'
The result is '10000B'. But since we have limited ourselves to 4-bits, the '1' (in the colored box) is ignored, or "dropped." The remaining 4-bits are the answer, 0. Therefore, '-1' can be represented by '1111B'. It meets all of our requirements: the most significant bit tells us the sign of the number, and it works with addition. Likewise, we should be able to find a bit pattern for '-2' by asking the question 'What bit pattern added to '0001B' gives us '1111B' (-1)? The answer, as you know, is '1110B'. Here is a new number line which shows this numbering scheme.
With this representation, known as two's complement, we can represent the negative numbers from -8 to -1, whereas with signed magnitude we could only represent -7 to -1. You can see that the bit pattern which used to represent '-0' (whatever that is) has been used to extend the negative range. Also, all negative numbers have the most significant bit set to '1'. What about addition? Does it work with the "regular" binary addition algorithm? The answer is 'yes.' However, there are some limitations of which we must be aware.
|
|
|
|
0010 |
00010
|
000010
|
00000010
|
1000
|
11000
|
111000
|
11111000
|