Pages: <Previous  22    23    24    25    26    27    28    29    30    31    32  Next >

7 Binary arithmetic

7.3 Subtracting 2's complement integers

You will probably have carried out subtraction of denary numbers using rules for subtraction that include the process of ‘borrowing’ whenever you need to subtract a larger digit from a smaller one. It is possible to perform binary subtraction in a very similar way, but that is not what happens in computers. The processor contains the circuits needed to perform addition, and it is much more efficient to use these circuits also to perform subtraction than it is to build in extra circuits to perform subtraction.

But how can subtraction be converted into addition? The answer is by first converting the number to be subtracted into its additive inverse. For example, the denary subtraction

7 − 5

can be converted into addition provided the additive inverse of 5 is used. As I mentioned in Section 3.4, the additive inverse of 5 is −5, and so the equivalent addition is

7 + (−5)

In 2's complement binary arithmetic, the additive inverse of a number is known as its 2's complement. I'll start, therefore, by showing you how to find the 2's complement of any binary number.

7.3.1 Finding the 2's complement

In Section 2.4 you saw how to find the 2's complement representation of any given positive or negative denary integer, but it is also useful to be able to find the additive inverse of a 2's complement integer without going into and out of denary. For instance, 1111 1100 (−4) is the additive inverse, or 2's complement, of 0000 0100 (+4), but how does one find the additive inverse without converting both binary integers to their denary equivalents?

The answer is that the additive inverse, or 2's complement, of any signed binary integer can be found by a two-step process: first find the complement (1's complement) of the given number and then add 1. 1's complement or complement means that all the 1s are changed to 0s and all the 0s to 1s.

An example should make this clear.

Example 8

Find the 2's complement of the signed integer 0001 1011.

Answer

First find the complement of the given integer (change all the 1s to 0s and all the 0s to 1s), getting:

1110 0100

and then add 1 to get:

1110 0101

So the 2's complement of 0001 1011 is 1110 0101. (Check: the given integer is+27, and 1110 0101 is −27.)

Activity 24 (Self assessment)

  1. Write down the complement of 1010 0101.
  2. Find the 2's complement of the signed integer 1011 0111. Check your answer by converting both integers to denary.
  3. Find the additive inverse of the signed integer 0000 1111.

Now read the answer

7.3.2 Subtraction

As I indicated at the start of this section, subtraction is converted to addition by replacing the number to be subtracted by its additive inverse, which in the case of binary arithmetic is its 2's complement. An example should make this clear.

Example 9

Subtract the signed integer 1010 1010 from the signed integer 0001 0110.

Answer

The additive inverse of the number to be subtracted, 1010 1010, is 0101 0101 + 1 = 0101 0110. Using this additive inverse transforms the computation to an addition:

(Check: 1010 1010 is −86 and 0001 0110 is 22, so the calculation is 22 − (−86), which is 108, and 0110 1100 is indeed 108.)

Activity 25 (Self assessment)

Carry out the following subtraction by first finding the additive inverse of the number to be subtracted:

1100 1010 − 0000 1110

Now read the answer

Pages: <Previous  22    23    24    25    26    27    28    29    30    31    32  Next >