# Section3.5Mathematical Meaning of C and V Flags.¶ permalink

The results from Section 3.2 and Section 3.4 yield the following rules when adding or subtracting two $n$-bit numbers:

• If your algorithm treats the result as unsigned, the carry condition flag (C) is zero if and only if the result is within the $n$-bit range; V is irrelevant.

• If your algorithm treats the result as signed, the overflow condition flag (V) is zero if and only if the result is within the $n$-bit range; C is irrelevant.

The CPU does not consider integers as either signed or unsigned. Both C and V are set according to the rules of binary arithmetic by each arithmetic operation. The distinction between signed and unsigned is completely determined by the program. After each addition or subtraction operation the program should check the state of C for unsigned integers or V for signed integers and at least indicate when the sum is in error. Many high-level languages do not perform this check, which can lead to some obscure program bugs.

The codes used for both unsigned integers and signed integers are circular in nature. That is, for a given number of bits, each code “wraps around”. This can be seen pictorially in the “Decoder Ring” shown in Figure 3.5.1 for three-bit numbers.

# Subsection3.5.1Exercises

##### 1

Use the “Decoder Ring” in Figure 3.5.1 to perform the following arithmetic. Indicate whether the result is “right” or “wrong”.

1. Unsigned integers: $\hex{1 + 3}$

2. Unsigned integers: $\hex{3 + 4}$

3. Unsigned integers: $\hex{5 + 6}$

4. Signed integers: $\hex{(+1) + (+3)}$

5. Signed integers: $\hex{(-3) - (+3)}$

6. Signed integers: $\hex{(+3) - (+4)}$

Hint Solution
##### 2

Add the following pairs of 8-bit numbers (shown in hexadecimal) and indicate whether your result is “right” or “wrong.” First treat them as unsigned values, then as signed values (stored in two's complement format).

1. $\hex{55 + aa}$

2. $\hex{55 + f0}$

3. $\hex{80 + 7b}$

4. $\hex{63 + 7b}$

5. $\hex{0f + ff}$

6. $\hex{80 + 80}$

Hint Solution
##### 3

Add the following pairs of 16-bit numbers (shown in hexadecimal) and indicate whether your result is “right” or “wrong.” First treat them as unsigned values, then as signed values (stored in two's complement format).

1. $\hex{1234 + edcc}$

2. $\hex{1234 + fedc}$

3. $\hex{8000 + 8000}$

4. $\hex{0400 + ffff}$

5. $\hex{07d0 + 782f}$

6. $\hex{8000 + ffff}$

Hint Solution