Back

Two's complement and Sign and Magnitude

Negative binary numbers
We have seen how to represent numbers using binary, Hex and BCD. We have only seen how to represent positive numbers. There will often be times when we want to store negative numbers. For example, we may need to record negative temperatures, or we could be writing a 'bank account' program where people's bank balances are stored, and they're sometimes overdrawn, or negative! There are two systems we need to know about; 'sign and magnitude' and 'twos complement'. They each have advantages and disadvantages.

Sign and magnitude
Suppose I want to record the weather temperature at 12.30pm on one day in degrees C in one byte. After some research, I've found out that I need to represent numbers from -50 to +50 and I've decided to store only whole numbers. Clearly, in the systems we've looked at so far, we have no way of recording negative numbers. I need to look again at my byte and redefine the bits in it. What I have decided to do is to use bit 7 (the bit on the far left), to represent the 'sign' of the number - whether it is positive or negative. In my system, I have decided that if bit 7 is set, then the number is negative. If it is reset, then this signals to me the number is positive. Bit 7 will only tell me the sign - it won't have a 'magnitude'. The other bits can keep the values they had before! (Incidentally, the bit on the far right is 'bit zero'! In one byte, you would talk about 'bit 0', 'bit 1', 'bit 2' and so on up to 'bit 7'). My new numbering system is now shown below!

This corresponds to -127. I can't represent anything smaller than this. Notice that bit 7 is now set, because the number is negative. This numbering system has a name. We talk about a sign and magnitude system, because the bits not only represent the size (or 'magnitude') of a number but one bit is also used to represent the sign of the number.

Now the sign and magnitude system is great for my purpose - to record one temperature. Using this system, I could now use two bytes to record the temperatures on 2 successive days if I wanted to. If I now wanted to work out the average of these temperatures, I'd need to begin by adding the two numbers together. For example, suppose I measured the temperature on two successive days and found them to be 23 degC and -16 degC. How could I add +23 to -16?

There is a small problem! We can't add 'symbols' (the + and the -) together in the same way that we can add 'amounts of something' together. It's a little bit like adding +23 and -16 in denary. You can add only the number parts together to get 39, and then you're not sure how to 'add' the symbols, and you end up with a wrong answer anyway! So, sign and magnitude is a nice, simple system for recording positive and negative numbers. If we want to do sums, however, we cannot use this system (easily)! We need a different one!

Two's complement
This system might seem strange at first but stick with it and follow the examples! I'm going to do away with the system of using bit 7 to show the sign of the number. I'm going to reallocate the 'worth' of each bit position in the following manner:

And how do you represent -12 using this system? There are a number of tricks you can use to quickly turn a negative number into it's two's complement form. My favourite is:

    1. Write down the number as a positive binary number.
    2. Starting with bit zero, copy all the bits up to and including the first set bit.
    3. Invert all the others.

For example, to convert -12 into it's two's complement form:

Back