Need clarification regarding carry and overflow bit

  • Thread starter Thread starter leo255
  • Start date Start date
  • Tags Tags
    Bit
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
3 replies · 2K views
leo255
Messages
57
Reaction score
2
I'm trying to understand carry & overflow bits. Carry seems pretty easy - If I'm not mistaken, if you carry on your last bit into a bit that you don't have, the carry flag is set to 1.

For overflow, I referred to another previous thread on here, of which one of the posters recommended the thread-starter look at the following link:

http://teaching.idallen.com/dat2343/00s/overflow.txt

This was very helpful to me, by clarifying that the overflow bit is the exclusive or (XOR), and is set to 1 when either (but not both) of the last bit or carry bit has a 1 carried into it. When they both have a 1 carried into them, it is set to 0.

I feel like I understand how to determine the overflow bit, but I don't understand what it means. The carry bit is easy: you are adding two numbers, and just don't have enough room (enough bits), so you don't get an accurate representation of the number. Just trying to get the same understanding for the overflow bit.

Thanks in advance for the help.
 
Physics news on Phys.org
Carry is mainly used for unsigned integers, overflow is something similar for signed integers.

If you add 0100 + 0101 (let's use4 bits here), you expect a positive result because both numbers are positive. Simply putting them into your CPU will give 1001, however - that number is negative. The carry bit is not set (as both numbers are positive) but the first bit got set -> overflow.

In the same way, adding two negative numbers should give a negative result.
Adding 1100 and 1100 gives (1)0100 where (1) is carry - a positive result! Carry bit is set, but the first bit is not -> overflow

The two cases for subtraction can be studied in the same way.
 
Carry is set for unsigned addition. For unsigned subtraction, it's usually a "borrow" bit, but on some processors it's the "not borrow" bit.

Overflow is for signed addition or subtraction, and indicates that the sign of the result is wrong due to overflow (addition) or underflow (subtraction). For a signed or unsigned multiply with a double register product, overflow isn't possible. For a signed or unsigned divide with a double register dividend and single register quotient, the quotient can overflow (including division by 0).