Binary arithmetic
Binary addition
We've now seen how to represent both positive and negative numbers in binary. Before we look at adding some numbers, let's look the rules of binary addition.
-
- Zero plus zero = zero! In binary, 0 + 0 = 0 (0 is the binary equivalent of zero)!
- One plus zero (or zero plus one) = one! In binary, 1 + 0 = 1 (1 is the binary equivalent of one)!
- One plus one = two! In binary, 1 + 1 = 10 (10 is the binary equivalent of two).
- One plus one plus one = 11 (11 is the binary equivalent of three).
- One plus one plus one plus one = 100 (100 is the binary equivalent of four).
- And so on.
Let’s look at some straightforward examples before we look at some more complicated ones later in the chapter.
Example 1
Add 25 to 20 in pure binary, using a byte for each number.
Example 2
Add 15 to 15 in pure binary, using a byte for each number.
The binary equivalent of 15 is 0000 1111
So we need to do the sum:
Example 3
Add 150 to 140 in pure binary, using a byte for each number.
The binary equivalent of 150 is 1001 0110
The binary equivalent of 140 is 1000 1100
So we need to do the sum:
Something strange has happened! We have generated a carry. Our answer needs to be in a byte, but we can’t hold the answer in a byte. We can if we have 9 bits, but a byte only has 8.
If ever you add some pure binary positive numbers together and you generate a carry, then you must disregard the answer. In this example, 1001 0110 + 10001100 gave us the answer 0010 0010 with a carry. Because a carry was generated when we did the addition, we know that the answer can’t be stored in a byte, is meaningless and should therefore be disregarded.
If this ever happens in a program, an error message would be displayed. The error message is known as an overflow error. It means that an answer was generated but it was too big to store in the data structure set up for the answer. Our carry bit in this instance could also be referred to as an overflow bit. It’s also possible to have an underflow error. This is generated when the answer is too small to be represented in the data structure set up for it.
Binary subtraction
Subtraction is the same as adding a negative number! For example,
-
- 10 - 5 is the same as 10 + (-5).
- 123 - 100 is the same as 123 + (-100).
- -23 -34 is the same as -23 + (-34).
We can use this property if we need to subtract numbers in binary because we know how to convert a negative number into its two’s complement. If we have to do the sum 23 - 5 (in binary, of course) then we will
-
- Rearrange it so that it reads 23 + (-5).
- Work out what 23 is as a two’s complement number.
- Work out what -5 is as a two’s complement number.
- Add 23 to -5.
So, any time we have to do a subtraction, we will rearrange the numbers so that it is an addition! Let’s look at some examples. It is worth noting before we begin that all numbers in the next examples, both positive and negative, will be in two’s complement form. This is actually quite important to note because it has an impact on the way we interpret any carry generated.
Example 1
Let's do the sum 28 - 16 = ? The first thing that we need to do is to rearrange it slightly.
28 - 16 = ? is equivalent to 28 + (-16) = ?
We have rearranged this sum for a very good reason. It involved a negative number. By putting the sum in the form 28 + (-16) we represent easily both 28 and also (-16), using the two’s complement numbering system. Once we have got both numbers into the two’s complement numbering system, we can then add them both together. Notice that we have converted (-16) into a two’s complement number using the three steps we met in the previous chapter. We
- Write down the positive version of the number.
- We copy all the bits from bit zero up to and including the first one bit.
- We then invert all the remaining bits.
The steps to doing this calculation are shown below.
Just to double-check, 0000 1100 is 8 + 4 = 12 in the denary system.
Note the ‘carry’ bit here. We are working with single bytes. Our answer is in a single byte. Unfortunately, when we did our sum, the one byte wasn't big enough to hold the answer. We had to use an extra bit, called a 'carry'.
The rules when a carry occurs with two’s complement arithmetic.
Sometimes, the carry tells us something. Sometimes we ignore it! How do you decide?
- When you add a positive number to a negative number, the rule is to "ignore any carry produced and accept the result as true". You saw that in the above example.
- If you add 2 negative numbers together, you will always get a carry. If bit 7 is a zero, then ignore the result. If bit 7 is a one, then accept the result.
- If you add two positive numbers together (in two’s complement form), you'll never get a carry. To check if the result is valid or not, you need to look at bit 7. Remember, in two's complement, all positive numbers begin with zero. If you add two positive numbers together, the result must be positive. You can check if an answer is out of the range that can be held by the byte by looking at bit 7. If it is a zero, accept it. If it is a one, reject the result.
Example 2
Let's do another sum. Lets do -80 - 40 = ? This is the same as -80 + (-40) = ?
We are adding two negative numbers together. We will always get a carry. Our rule says if you add two negative numbers together then check bit 7. If it's a one (as it is here), the result is good. Just to double-check the binary,
we have -128 + 8 = -120, so our answer is correct.
Example 3
Let's do another sum. Lets do -80 - 90 = ? This is the same as -80 + (-90) = ?
We are adding two negative numbers together. Our rule says if you add two negative numbers together then check bit 7 - if it's a zero, reject the result. Bit 7 is a zero so the result is meaningless.
Example 4
Lets do 96 + 50 = ?
We are adding two positive numbers together, so the result must be positive. We aren't checking the carry bit when we add two positive numbers, we're checking bit 7. In this case, bit 7 is a one, so we reject the result.
Summary
-
- When doing binary arithmetic, all numbers are put in two’s complement form unless you are told otherwise.
- You cannot simply accept a result. You must sometimes look at the carry and sometimes look at bit 7 to make a decision as to the validity of the result.
- If a number cannot be held in the data structure provided for it, then we say there is an ‘overflow’.