To perform the operation (A-B), we firstly modify our computation to (A-K)+(K-B)
where K = 2^{n} such that n is the number of digits (bits) in B, that is n = floor(log_{2}|B|) + 1,
and where (K-B) is the 2's complement of B.
Algorithm:
EXAMPLE:
- Given A and B,
- Complement B to produce (K-B),
- Add A and complement B,
- Subtract K from the result.
Add 27_{10} + (-11_{10}) 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 27_{10} to binary = 011011_{2}
Step 3: Convert 11_{10} to binary = 001011_{2}
NOTE: if we simply subtracted 011011_{2} by 001011_{2} we would expect to get 010000_{2} (= 16_{10})
Step 4: Get the 2's complement of 001011_{2} - i.e 110100 + 1 = 110101
Step 5: Perform the addition of the two representations:
011011 |
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:
010101 |
Which, after converting the second subtrahend to its 2's complement and changing the operation to addition, is equivalent to
010101 |
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?
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! |
---|
Last updated 2002/02/08
© J.A.N. Lee, 2000-2002.