## Binary subtraction

# Binary subtraction (or binary hell)

Right, binary subtraction lack the simplicity of the binary addition system and has a couple of unforgiving rules that must be followed. However, before entering the world of binary subtraction, let's take a close look at the difference between signed and unsigned bits.

## Signed and Unsigned numbers

When working with a 4-bit value you can create 16 different numbers. Now, unsigned values hold only positive numbers and a signed value can have both positive(+) and negative(-) values.

An unsigned 4-bit value ranges between 0 and 15 while a signed 4-bit value ranges between 7 and -8. This table will illustrate the relationship between signed and unsigned values.

Binary value | Unsigned | Signed |
---|---|---|

0000 | 0 | 0 |

0001 | 1 | 1 |

0010 | 2 | 2 |

0011 | 3 | 3 |

0100 | 4 | 4 |

0101 | 5 | 5 |

0110 | 6 | 6 |

0111 | 7 | 7 |

1000 | 8 | -8 |

1001 | 9 | -7 |

1010 | 10 | -6 |

1011 | 11 | -5 |

1100 | 12 | -4 |

1101 | 13 | -3 |

1110 | 14 | -2 |

1111 | 15 | -1 |

This is how unsigned bits works. The first digit to the left (Most Significant Bit[

**MSB**]) sets the sign of the number. It's an easy rule; if the MSB = 1 it's negative, if MSB = 0 then it's positive.

## The rules

There are two sets of rules.

First up, the strict rule of subtracting ones and zeroÂ´s:

Equation | Answer |
---|---|

0 - 1 | 0 |

1 - 0 | 1 |

1 - 1 | 0 |

10 - 1 | 1 |

This looks a lot like the rules for addition, and as in addition, these rules are binding.

The second set of rules describe how to make the actual calculations, and these rules look like this:

**1.**Maintain the amount of digits in each number

**2.**Invert the digits in the second value (1st complement)

**3.**Add 1 to the second values 1st complement (2nd complement)

**4.**Add the first value and the second value together

**5.**Cut loose any carried values

We'll depicture these 5 rules with the following example:

5

-3

=2or

101

- 11

=????

Now, let's start with the first rule and make real bits out of these numbers:

101 - 11 => 0101 - 0011

That's better, now they have the same amount of digits which is vital.

The second rule stipulates that we invert the digits in the second value. What this means is that we change all zeroÂ´s (0) to ones (1) and vice versa, like this:

(The second value)

This is called the first complement. But there are two complements, and the second follows right after. But first, let's see how the equation looks now:

0101

-1100

=????

According to the third rule we should add 1 to the second values first complement. This is known as the second complement and ensures that we get an exactly inverted number, like this:

1100

+0001

=1101

This makes more sense now since if we look in the chart, 1101 signed is -3.

Now that we have inverted the second value we can add them together instead of doing a subtraction. Here is how that works:

If you have the equation 5 - 3 you can also do this equation: 5 + (-3). This is what we do, we invert the second value so that we can add it to the first in it's negative form.

So now we add them like this:

As we can see, we use the 5th rule here as well and remove the carried value. This doesn't have to happen but can happen if you subtract a positive value from a negative value or vice versa, which we do here. If it happens the overflow is removed. Here we end up with the value 0010, which corresponds to decimal 2. Sounds right, since 5 - 3 = 2.

**Next page: The binary circel**

[breakpoint]

# The binary Circel

A binary value can be depictured as a circle since it has a beginning and an end. Let's create an 4-bit value in circle form:

This might take some explanation. The picture isn't great and it all got a bit cramped, but it shows the positive flow and the negative flow. It also marks the bit values (0000 to 1111).

This circle can be used to solve subtraction in a more visual way. We talked earlier how bits that started with a 0 was positive, and bits that started with a 1 was negative. This picture of course isn't accurate in that sense, but it will give us a visual idea of how to use it.

A simple way to do subtraction with this circle is by moving inside the circle. Let's do some subtraction

**2 - 7**

If we do 2 - 7 in the circle, then we first analyze the numbers we're dealing with. We split them up in to +2 and -7. In the circle, positive numbers move to the left and negative to the right.

**Huh!?**

This picture will illustrate:

Granted that this might be the ugliest picture yet, it proves that the circle works. First we move two step to the left (since we had the value +2), and then 7 steps to the right (since we had the value -7). This lands us on the number -5 which happens to be correct!

This picture however is not great since it looks like you could do an equation like 2 - 11 and get the answer -9 which in this circle is 0111, thus the positive number 7.

This correct 4-bit circle however can give us a more accurate circle to work with:

This circle displays a signed 4-bit value ranging from 7 to -8. Now let's see what happens when we use subtraction with binaries. Let's invert the number 2.

1st complement stipulates that we invert the value, in this case 2, or 0010. When inverted that is 1101. Let's look in the circle:

Yes, there it is. Problem is, 2 inverted in binaries translates into -3 in decimal.

__What the hell!__Well, since we start counting positive and negative numbers from different starting values (0 and -1) we have to compensate for the missing value (1).

Enter two's complement. The 2nd complement stipulates that 1 should be added to the inverted number, thus giving us the same decimal number (but a different binary value).

Look:

And with two's complement, we get the correct number, -2.

That's how to use the binary circle, and hopefully that somewhat explains the use of two's complement.

## Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.