Subtraction and Two’s Complement

It is possible to handle subtraction in much the same way as we’ve covered addition – first create a half subtracter then a full subtracter and combine several of the latter.  However we don’t need to do that if we understand how negative numbers are usually handled.

For the sake of a simple example let’s assume we’re only dealing with 4-bit numbers.  With two’s complement we can handle the range of numbers -8 to +7.  The positive integers would be the values from 0001 to 0111 (ie 1 to 7) and the negative numbers would be the values from 1000 to 1111.  The negative numbers effectively count back towards zero so 1000 would be -8 and 1111 would be -1.  (Hopefully it’s obvious we’d use 0000 to represent 0.)

Now that may not seem that useful but we do need to be able to handle positive and negative numbers.  Having introduced the concept we can make use of it for subtraction since A-B is the same as A+(-B).

Consider 7 minus 5.  7 is 0111 and -5 is 1011.  Add them together and we get   (1)0010 which, if we ignore the initial overflow bit, gives us the answer 2.

However this does mean we need a way of negating a number within our number range and with two’s complement all we need to do is XOR each bit with 1 before finally adding 1 to the overall result.  So for 4 the positive value is 0100 and XORing gives 1011.  Adding 1 to 1011 gives 1100 which is the two’s complement for -4.

Obviously we would normally be working with numbers with more bits.  Also remember that what we’re dealing with here is the hardware of a computer – the software can handle arithmetic in its own way but underlying it all is the hardware that has to be able to perform basic operations with memory etc.

It is also worth noting that we have a negative number if and only if the first bit of the binary version is 1.  That potentially gives us a quick way of checking for a negative number.

Leave a comment