Binary arithmetic - overflow and carryout at same time?

In binary arithmetic, When you subtract 2 signed numbers you must discard the carry out. My question is, is it possible for overflow to occur and a carry out? So, on paper there would be two extra bits and you would have to discard one?

Also, if your subtracting 2 signed numbers, how do you differentiate between overflow and a carry out?

When you subtract two signed numbers, the Carry flag is irrelevant; the Overflow flag is all that counts. The Carry flag is for unsigned integer operations. So there is only ever one bit to worry about.

Overflow and carry out are philosophically the same thing. Both indicate that the answer does not fit in the space available. The difference is that carry out applies when you have somewhere else to put it, while overflow is when you do not. As an example, imagine a four bit computer using unsigned binary for addition. If you try to add $1010_2+111_2$ without the word length restriction, you get $10001_2$ The high bit does not fit in our word. If this word is all the space allocated to this variable, it represents overflow as the result is too large to represent. If we have a two word space allocated to this variable, it becomes a carry out and is stored in the higher word. We would then represent the addition as $0000\ 1010_2+0000\ 0111_2=0001\ 0001_2$ and the carry is explicit.

• Overflow and Carry are not the same thing! Overflow indicates that a signed result is too big or too small to fit in the destination; Carry indicates that an unsigned result is too big to fit in the destination. So for instance on a typical 8-bit processor, adding the hex values 0xE0 and 0x40 will set the Carry flag but not the Overflow flag. – TonyK Feb 7 '16 at 16:00
• @TonyK: I don't know about the flags in a processor, but the idea is the same. If you underflow a subtraction, you can fix that with a borrow if there is something to borrow from. Similarly, a signed add can carry or overflow if there is not enough room. I have added 'philosophically' to indicate that I am not speaking about flag behavior. Feel free to add your answer, which may be very helpful – Ross Millikan Feb 7 '16 at 16:02
• But whether a subtraction underflows depends on whether you are interpreting the numbers involved (the operands and the result) as signed or unsigned. – TonyK Feb 7 '16 at 16:04

Yes, you can have an overflow and a carry flag in the same operation. This is because an overflow flag is hooked up to an XOR gate, where the inputs are the carries from the two bits on the left. The carry flag gets triggered if there is a carry on the left most bit (the signed bit for signed numbers). So if you have a carry on the left most bit, but not on the one second from the left, you have an overflow and a carry. If you have a carry on the bit second from the left and not on the left most bit, you have only an overflow.

Here's a picture of an adder/subtractor where the V is the overflow, and the C is the carry. Each bit of the sum is calculated using a full adder, and each bit may have a carry. V is an XOR gate connected to the two left most carries. https://i.stack.imgur.com/aYD9L.png

What the overflow represents is when an operation causes the sum to fall outside the maximum or minimum bounds. For example, with an 8 bit signed operation, the maximum number of the sum is 255. while the minimum is -256.

So if you try to do 255 + 1 (01111111 + 00000001) you get an overflow. Notice how there was a carry on the second bit from the left, but not the left most. The sum comes out as -256, which is wrong.

An example where you get both an overflow and a carry would be -64 + (-256) (11000000 + 10000000). There is a carry on only the left most bit. The sum comes out as 64, which is wrong.

An example where you get only a carry would be -64 + 64 (11000000 + 01000000). There is a carry on both the left most and second from the left. The sum comes out as zero, which is right.

New contributor
Chris Hamilton is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

In processors, overflow flag indicates that sign bit has been changed during adding or subtracting operations But carry flag means adding or subtracting two registers has carry or borrow bit.

• I think the question is more about binary arithmetic as a concept rather than specific flags on processors, also the question is can both overflow and carry occur at the same time. – Alex J Best Apr 18 '19 at 23:42