The number of bits used to represent a value is determined at the time a program is written. So when performing arithmetic operations you cannot simply add more digits (bits) if the result is too large, as you can do on paper. You saw in Section 3.1 that the C indicates when the sum of two unsigned integers exceeds the number of bits allocated for it.

In Section 3.3 you saw that carry is irrelevant when working with signed integers. In this section you will see that adding two signed numbers can also produce a result that exceeds the range of values that can be represented in the allocated number of bits.

The flags register, CPSR, provides a bit, the overflow condition flag (V), for detecting whether the sum of two $n$-bit, signed numbers stored in the two's complement code has exceeded the range allocated for it. Each operation that affects the overflow condition flag sets the bit equal to the exclusive or of the carry into the highest-order bit of the operands and the ultimate carry. For example, when adding the two 8-bit numbers, $\hex{15}$ and $\hex{6f}\text{,}$ we get:

In this example, there is a carry of zero and a penultimate carry of one. The V flag is equal to the exclusive or of carry and penultimate carry:

where ‘^’ is the exclusive or operator. In the above example:

There are three possibilities when adding two numbers:

1. The two numbers are of opposite sign.

We will let $x$ be the negative number and $y$ the positive number. Then we can express $x$ and $y$ in binary as: \begin{gather*} x = \binary{1} \dots\\ y = \binary{0} \dots \end{gather*} That is, the high-order bit of one number is $\binary{1}$ and the high-order bit of the other is $\binary{0}\text{,}$ regardless of what the other bits are.

Now, if we add $x$ and $y\text{,}$ there are two possible results with respect to carry:

1. If the penultimate carry is zero: This addition would produce V = 0 ^ 0 = 0.
2. If the penultimate carry is one:

This addition would produce V = 1 ^ 1 = 0.

We conclude that adding two integers of opposite sign always yields $\binary{0}$ for the overflow condition flag.

2. Both numbers are positive.

Since both are positive, we can express x and y in binary as: \begin{gather*} x = \binary{0} \dots\\ y = \binary{0} \dots \end{gather*} That is, the high-order bit of both numbers is $\binary{0}\text{,}$ regardless of what the other bits are.

Now, if we add $x$ and $y\text{,}$ there are two possible results with respect to carry:

1. If the penultimate carry is zero:

This addition would produce V = 0 ^ 0 = 0. The high-order bit of the sum is zero, so it is a positive number, and the sum is within range.

2. If the penultimate carry is one:

This addition would produce V = 0 ^ 1 = 1. The high-order bit of the sum is one, so it is a negative number. Adding two positive numbers cannot yield a negative sum, so the sum has exceeded the allocated range.

3. Both numbers are negative. \begin{gather*} x = \binary{1} \dots\\ y = \binary{1} \dots \end{gather*} That is, the high-order bit of both numbers is $\binary{1}\text{,}$ regardless of what the other bits are.

Now, if we add $x$ and $y\text{,}$ there are two possible results with respect to carry:

1. If the penultimate carry is zero:

This addition would produce V = 1 ^ 0 = 1. The high-order bit of the sum is zero, so it is a positive number. Adding two negative numbers cannot yield a positive sum, so the sum has exceeded the allocated range.

2. If the penultimate carry is one:

This addition would produce V = 1 ^ 1 = 0. The high-order bit of the sum is one, so it is a negative number, and the sum is within range.