## Section3.3Arithmetic Errors—Unsigned Integers

¶It should be obvious to you that the binary number system is good for storing unsigned integers. Don't forget that it does not matter whether we think of the integers as being in decimal, hexadecimal, or binary since they are mathematically equivalent. If we are going to store integers this way, we need to consider the arithmetic properties of addition and subtraction in the binary number system. Since a computer performs arithmetic in binary, we might ask whether addition yields arithmetically correct results when representing decimal numbers in the binary number system.

We will use four-bit values to simplify the discussion. Consider addition of the two unsigned integers, 2 and 4:

\(\binary{0010}_{2}\) | \(=\) | \(2_{10}\) | |

+ | \(\binary{0100}_{2}\) | \(=\) | \(4_{10}\) |

\(\binary{0110}_{2}\) | \(=\) | \(6_{10}\) |

and `C = 0`

. That is, the result fits within the four bits, so the carry condition flag is zero. Notice that the computer is doing the addition with the binary representation of the decimal numbers (shown both in binary and hex here). So far, everything looks good.

Next, let us consider two larger integer, keeping our four-bit storage space. We will add the two unsigned integers, 4 and 14:

\(\binary{0100}_{2}\) | \(=\) | \(4_{10}\) | |||

+ | \(\binary{1110}_{2}\) | \(=\) | \(14_{10}\) | ||

\(\binary{0010}_{2}\) | \(=\) | \(2_{10}\) | \(\ne\) | \(18_{10}\) |

and `C = 1`

. The result has exceeded the four bits that we allocated for storing the integers. The CPU indicates this by setting the carry condition flag to one.

Now, let us consider subtracting 14 from 4:

\(\binary{0100}_{2}\) | \(=\) | \(4_{10}\) | |||

- | \(\binary{1110}_{2}\) | \(=\) | \(14_{10}\) | ||

\(\binary{0110}_{2}\) | \(=\) | \(6_{10}\) | \(\ne\) | \(-10_{10}\) |

and `C = 1`

. We had to borrow from outside the four bits. The CPU indicates this by setting the carry condition flag to one.

From the discussion in Section 3.1 it is easy to see that these four-bit arithmetic examples generalize to any size arithmetic performed by the computer. After adding two numbers, the carry condition flag will always be set to zero if there is no ultimate carry, or it will be set to one if there is ultimate carry. Subtraction will set the carry condition flag to zero if no borrow from the “outside” is required, or one if borrow is required. These examples illustrate the principle:

When adding or subtracting two unsigned integers, the result is arithmetically correct if and only if the carry condition flag (

`C`

) is set to zero.

It is important to realize that the `C`

flag in the `CPSR`

register is always set to the appropriate value, \(\binary{0}\) or \(\binary{1}\text{,}\) each time an addition or subtraction is performed by the CPU. In particular, the CPU will not ignore the `C`

flag when there is no carry; it will actively set it to zero.