Binary Subtraction

To perform the operation (A-B), we firstly modify our computation to (A-K)+(K-B)
where K = 2n such that n is the number of digits (bits) in B, that is n = floor(log2|B|) + 1,
and where (K-B) is the 2's complement of B.

Algorithm:

  1. Given A and B,
  2. Complement B to produce (K-B),
  3. Add A and complement B,
  4. Subtract K from the result.

EXAMPLE:

Add 2710 + (-1110) using complementary representation for the negative value.

Step 1: Choose a fixed length representation size that will encompass the domain of work - in this case, let us choose 6 digits (bits)

Step 2: Convert 2710 to binary = 0110112

Step 3: Convert 1110 to binary = 0010112
NOTE: if we simply subtracted 0110112 by 0010112 we would expect to get 0100002 (= 1610)

Step 4: Get the 2's complement of 0010112 - i.e 110100 + 1 = 110101

Step 5: Perform the addition of the two representations:

011011
+ 110101
_______
1010000

Step 6: Restrict (truncate from the left) the answer to six bits, to give the answer 010000.   WHY?

So if we can add the representations of a positive number and a negative number, then we can simulate subtraction by:

  1. CONVERT the second addend to its two's complement;
  2. ADD the two number representations.

Considerations:

Then think about this problem:

Reverse the example above:

010101
- 011011
_______
??????

Which, after converting the second subtrahend to its 2's complement and changing the operation to addition, is equivalent to

010101
+ 100101
_______
0111010

In this case the extra digit in the result is 0; what does this indicate? Or should we just ignore it again and retain only the lower 6 bits?

CAVEAT EMPTOR
Let us emphasize here that in these algorithms for arithmetic we work with REPRESENTATIONS not VALUES. These are algorithms which reliably simulate arithmetic inside a computer; they are NOT mathematical formulae!

[Prev][TOC][Next]


Last updated 2002/02/08
© J.A.N. Lee, 2000-2002.