Related Tags

architecture
binary
numbers
arithmetic

# How to subtract two binary numbers

In computer architecture, two binary numbers are subtracted using addition which avoids the need for a separate hardware to perform subtraction. For example, if $5-3$ is to be calculated, then the operand on the right side ($3$) will need to be converted into its negative counterpart ($-3$) and added to the other operand ($5$).

The two’s complement representation is the standard format for negative numbers.

## Example

The steps below are to be followed when subtracting $17$ from $9$:

1. Convert both the numbers into binary format (the binary conversion table can be used).

$9 = (1001)$2

$17 = (10001)$2

1. Make sure that both of the binary numbers have the same number of bits. If they do not, the number with the lesser bits will be extended. This is done by adding extra $0$s to the left​ of the shorter number until the sizes become equal. In our example, $9$ has four bits while $17$ has five bits, so we add one more $0$ to the left making $9 = (01001)$2.

2. Convert $17$ into the two’s complement representation of $-17$ by inverting all of the bits and adding $1$.

1. Add $9$ and $-17$ while ignoring the extra overflow bit from the answer.

When subtracting a larger number from a smaller one, we have to invert the ​bits of the answer and add $1$ to find the magnitude. T​he sign is already known to be negative.

Magnitude: $(01000)$2 $= 8$.

RELATED TAGS

architecture
binary
numbers
arithmetic