Http://www



Negative Numbers and Binary Subtraction

In order to perform subtraction, it is necessary to understand negative numbers, especially as it pertains to binary representation. Four different systems for representing negative integers in binary have been used at one time or another. Most are still in use, as we will see.

The simplest is called signed magnitude. In this the left–most bit is simply turned on (made high, or 1) and the remaining bits hold the absolute value of the number. For example, 10010100 is negative because the left most bit is a 1. There are severe limitations to this method and it is not used often but has the advantage of making it easy to see that a value is negative.

The next most common is a method called one’s complement. In this method, all of the bits are simply inverted, i.e. 1’s become 0’s, and 0’s become 1’s. Thus, the binary number 01101001 becomes 10010110. It has an advantage similar to signed magnitude in that the left most bit is 1 when the value is negative. But no one uses one’s complement directly.

The third system, two’s complement, also uses the left-most (most significant) bit to easily identify a negative number, and uses one’s complement in part, but adds a value. This value is one. To convert an integer into it’s negative counterpart, invert all of the bits as in one’s complement, and then increment, or add the value of one.

For example, the value 2710 is 000110112 using 8 bits. The two’s complement of 27, or -27, is obtained by inverting the bits, to get 11100100, and then incrementing, or adding the value one to get 11100101.

00011011 2710

11100100 invert all of the bits

1 increment, or add 1

--------

11100101 result

If the result of the increment results in a carry after the left-most bit, that bit is simply discarded with no impact on the value.

One of the more interesting details is that values processed by two’s complement can be reversed by applying two’s complement. (The technical term for that is reflexive.)

11100101 -2710

00011010 inverts all of the bits

1 increment, or add one

--------

00011011 result is back to 2710

Notice that in all cases a bit has been lost from the possible size of the value in order to use accommodate the range of negative values. This means that a 16 bit integer can either be of the range 0 to 65536, or -32767 to 32767.

Binary values created using two’s complement can be used for addition as well as multiplication. The easiest way to subtract two numbers is to make the subtrahend negative. We can see this if we consider the following example:

7 – 4 = 7 + (-4) (hint: the 4 is the subtrahend)

Using this method engineers and computer designers do not need to create a separate subtraction component in the ALU (arithmetic logic unit).

Performing Subtraction

Using four bit values, mostly because the number of bits aren’t too many, let’s perform 7-4. Using the example from above, we will convert the 4 to -4 and add.

0100 4

1011 invert

1 add one

----

1100 -4 using two’s complement

(An interesting coincidence is that using two’s complement and only 4 bits, -4 looks just like -4 would using signed magnitude – but don’t be fooled! Using 8 bits, -4 would be 111111002)

Adding the two values in binary produces the following:

0111 7

+1100 -4

----

10011

^ extra carry bit

We can discard the extra carry bit because it exceeds the number of bits we are using to define our range. 00112 is 3 in base 10. So it seems to work.

Using eight bits (with a range of ±127), let’s try another example 27-13.

00011011 27

00001101 13

11110010 invert

1 add one

11110011 -13 using two’s complement

00011011 27

+11110011 -13

-----------

100001110 sum/result

^ extra carry bit (discard)

00001110 14

Negative Results

It is quite possible to get a negative result. You would get one if you attempted to subtract 27 from 13, or 13-27. Given the last example, we would see that the result should be -14, but how can you get that result, and how can you recognize that it is a negative value? Using the same method as before, we create both 13 and -27:

00001101 13

00011011 27

11100100 invert

1 add one

11100101 -27 using two’s complement

00001101 13

+11100101 -27

---------

11110010

Notice that the left most bit (of the right most eight bits – there was no extra carry this time) is a 1. That is the give-away that the result is negative. To convert this to a decimal number we can recognize, we can apply two’s complement to this value.

11110010 result of the addition above

00001101 invert

1 add one

00001110 result (=14)

Brain Teaser

I wrote earlier that eight bits has a range of ±127. But this is only 255 values (-127 to -1, 1 to 127, plus 0), not 256 that eight bits lets us use. Which value is missing? Why is it missing?

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download