# Subtraction of Binary Numbers.

Binary subtraction, is a similar process to decimal subtraction.
To perform the binary subtraction of two numbers. Carry out the subtraction for each pair of corresponding bits, starting from the l.s.b. to m.s.b.,
using he following rules;

- 0 - 0 = 0.
- 1 - 0 = 1.
- 0 - 1 = 1 Subtract 1 from next column.
- 0 - 1 - 1 = 0 Subtract 1 from next column.
- 1 - 1 - 1 = 1 Subtract 1 from next column.

## Twos Complement Notation.

An alternative method for carrying out subtraction involves a format called twos complement. Twos complement notation is a way of representing
negative numbers in binary.

( If we add a negative number to a positive number, it is the same as subtracting one positive number from another. e.g. )

6 + (-2) = 6 - 2

6 – 2 = 4

So first the binary number to be subtracted is converted to a negative number using two’s complement notation. Then the two numbers are added
as normal, however any carry produced when adding the m.s.b. of each number is ignored. The result we obtain is the same as if we had subtracted
the original number that was converted to twos complement notation.

One important point about this method is that we must decide on the number of bits we are going to use, (e.g. 4 bit, 8 bit, 16 bit etc) and use the
same number of bits to write each number (using leading zeros when required). e.g. If we are performing twos complement calculations with 8 bit
numbers, then **1** must be written as **0000 0001** etc.

When using twos complement you will see that ;

**If the most significant bit is one, then the number is negative.**
**If the most significant bit is zero, the number is positive.**
**Therefore positive values for an eight bit number, range from 0000 0000 to 0111 1111.**

## Converting a number into twos complement notation.

To convert a positive binary number to a negative number using twos complement notation;

- invert all the bits (i.e. change 1s to 0s and 0s to 1s).
- add 1 to the inverted number
**and ignore the final carry.**

Example: Write -4 decimal in 8 bit twos compliment format.

- 4 decimal = 0100 bin.
- Using 8 bits = 0000 0100.
- Invert bits = 1111 1011.
- Add 1: 1111 1011 + 1 = 1 1111 1100.
- Ignore final carry (i.e. ninth bit) gives 1111 1100.
**Therefore -4 decimal in binary (8 bit) twos compliment = 1111 1100. **

## Example of using twos compliment for subtraction in binary.

5 - 4 = 1.

5 decimal = 0000 0101 as an 8 bit binary number.

From above -4 decimal = 1111 1100 as an 8 bit binary number in twos compliment form.

The result above is 1, which is the correct result of subtracting 4 decimal (100 bin) from 5 decimal (101 bin).