Difference between the carry flag and overflow flag

  • Context: Engineering 
  • Thread starter Thread starter Fatima Hasan
  • Start date Start date
  • Tags Tags
    Difference
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 9K views
Fatima Hasan
Messages
315
Reaction score
14
Homework Statement
Why does the carry flag not work for signed number addition and the overflow does not work for unsigned numbers?
Relevant Equations
-
Here's my explanation with an example:
(d: decimal )
7d + 2d = 9 d
If signed:
the result indicates an overflow, because it exceeds the maximum which is +7.
( 0111 + 0010 = 1001 )
However, CF = 0 indicating that there is no overflow which is wrong.
OF = 0 ⊕ 1 = 1 -> invalid result, because the result exceeds the maximum.
If unsigned:
the result is valid because min= -8< 9 < max= 7
CF = 0
OF = 0 ⊕ 1 = 1 -> indicates that the result is invalid which is incorrect.
Is there any further explanations ?
 
Last edited:
on Phys.org
Fatima Hasan said:
Homework Statement:: Why does the carry flag not work for signed number addition and the overflow does not work for unsigned numbers?
Relevant Equations:: -

Here's my explanation with an example:
(d: decimal )
7d + 2d = 9 d
If signed:
the result indicates an overflow, because it exceeds the maximum which is +7.
( 0111 + 0010 = 1001 )
However, CF = 0 indicating that there is no overflow which is wrong.
OF = 0 ⊕ 1 = 1 -> invalid result, because the result exceeds the maximum.
If unsigned:
the result is valid because min= -8< 9 < max= 7
CF = 0
OF = 0 ⊕ 1 = 1 -> indicates that the result is invalid which is incorrect.
Is there any further explanations ?
It looks like your examples assume 4-bit numbers, and either signed or unsigned addition. As a 4-bit signed number, the possible values range from -8 to + 7, inclusive. As a 4-bit unsigned number, the possible values range from 0 to 15, inclusive.
Here's what the Intel documentation says about CF and OF, which may or may not have some bearing on your question.
Carry flag — Set if an arithmetic operation generates a carry or a borrow out of the most significant bit of the result; cleared otherwise. This flag indicates an overflow condition for
unsigned-integer arithmetic.

Overflow flag — Set if the integer result is too large a positive number or too small a negative
number (excluding the sign-bit) to fit in the destination operand; cleared otherwise. This flag
indicates an overflow condition for signed-integer (two’s complement) arithmetic.

The status flags allow a single arithmetic operation to produce results for three different data types: unsigned integers, signed integers, and BCD integers. If the result of an arithmetic operation is treated as an unsigned integer, the CF flag indicates an out-of-range condition (carry or a borrow); if treated as a signed integer (two’s complement number), the OF flag indicates a carry or borrow;
In your case, you're working with a hypothetical processor with 4-bit registers, together with signed or unsigned arithmetic.
If the operation is unsigned addition, and the numbers being added are 7 and 9, the result is too large to fit in 4 bits, so the CF flag would be set. It's true that the result overflows the capacity of a 4-bit register, but the result is also producing a carry at bit 4 (with bits numbered 0 through 3 in a 4-bit number). For unsigned subtraction, 9 - 7 wouldn't cause a problem, but subtracting 9 from 7 would cause CF to be set, because there would need to be a borrow from bit 4.

If the operation is signed addition, if the addition of two positive numbers produces a negative result, OF is set (e.g. 7 + 1 produces a bit pattern that represents a negative number). Also, if the addition of two negative numbers produces a positive result, OF is set in this case, as well (e.g., -3 + -6 produces a bit pattern that represents a positive number).
 
  • Like
Likes   Reactions: sysprog