Subtraction in binary may be carried out by using a process called Two’s Complement.
Two’s complement is a mathematical operation on binary numbers, as well as a representation of signed binary numbers based on this operation. The two’s complement of an N-bit number is defined as the complement with respect to 2N, in other words the result of subtracting the number from 2N. This is also equivalent to taking the ones’ complement and then adding one, since the sum of a number and its ones’ complement is all 1 bits.
The two’s complement of a number behaves like the negative of the original number in most arithmetic, and positive and negative numbers can coexist in a natural way. As (a – b) is the same as ( a + ( -b )) we can use this trick to perform subtractions with negative numbers and additions.
Negating a binary number uses the Flip and Add method. All the bits in the number are flipped; which is to say 0 becomes 1 and 1 becomes 0. (See the section on logical NOT for more information.) 1 is then added to this number to give the negative. For example:
All binary numbers must use the same number of digits. Numbers are padded with zeroes at the left where necessary.
When working with negative numbers the range of numbers changes. For example an 8 bit number goes from 00000000 to 11111111 or 0 to 255. If negative numbers are used then the range changes. The 8 bits still have 256 possible values but the range changes to values between 127 and -128.
Example: Subtract 28 from 91. Remember to convert to binary first!
91 – 28 is 01011011 – 00011100. Note that both numbers are padded to eight bits.
To convert 28 to -28 we flip the bits and add 1.
Flipping gives us 11100011. Adding 1 is 11100100.
The sum is now 01011011 + 11100100 which is 00111111 or 63.
The extra carry at the end simply “falls off the end”. The answer stays at eight bits.
Next: Hexadecimal Arithmetic