What is the diffrence between a carry bit and overflow
-
$\begingroup$ What have you tried so far? The title contains different question from body. Reading flags is strictly programming task, so off-topic here. $\endgroup$– EvilFeb 10, 2018 at 16:42
-
$\begingroup$ en.wikipedia.org/wiki/Status_register $\endgroup$– HEKTOFeb 10, 2018 at 17:01
-
$\begingroup$ This video solved my problem. Hopefully it does for you too: youtu.be/9cXe_T99nL4 $\endgroup$– a-kihliFeb 10, 2018 at 17:03
-
$\begingroup$ you should mention the architecture you are referring to. $\endgroup$– Ran G.Feb 10, 2018 at 18:36
-
$\begingroup$ @RanG. If the answer depends on any particular architecture, it's hard to see it being on-topic here. $\endgroup$– David RicherbyFeb 11, 2018 at 13:10
1 Answer
Carry is generally used for unsigned arithmetic and overflow is used for signed arithmetic.
This unsigned 8-bit operation results in Carry, but no overflow (the sign of the result is correct):
0xC0 + 0xD8 = 0x98
If we're doing unsigned 8-bit arithmetic, that's fine and we're only interested in the carry bit in this case, which tells us that the "correct" answer is actually 0x198
.
If we look at the same operation and the same operand values, but consider that the operands are signed, we have the equivalent:
-0x40 + -0x28 = -0x68
In this case, there is also no "overflow" and the result of the arithmetic is correct.
Consider this unsigned operation, however:
0x70 + 0x68 = 0xD8
No overflow here -- and no carry. The unsigned arithmetic is simple. But now look at the operands as signed values:
0x70 + -0x98 = 0x28
The answer is obviously wrong. We have subtracted a larger number from a smaller one and ended up with a positive value. The carry is not set here, so it doesn't offer a clue of the problem. The overflow flag reveals that the "correct" signed answer is -0x28
.