When is a bit CARRY and when is it OVERFLOW?

in dealing with UINT at a bitwise level

I can add, sub, mult and divide them and in each case determine if the result would no longer “fit” in the defined datatype… To me that is Over/Undeflow (depending on the operation)

But I have a calculator that seems to indicate BOTH Carry and Overflow in some cases

1111 + 0001 = [1]0000  

Yeah that one makes sense as it IS both
Its a carry OUT of the high bit
And also the result overflows

Basically the CARRY out causes OVERFLOW

but when would you have a CARRY and NOT Overflow
and when would you have OVERFLOW and NOT Carry

I’m thinking that “Sign” function comes into play here

Yeah I dont know the Casio well enough to say

I dont know if multiplication could generate an overflow without a carry
Carry is usually just the high bit rolling out
0x8 + 0x8 -> 0x10
Overflow is, usually, the result of any result thats too large
And it could be several bits that roll out
0x8 * 0x8 -> 0x40
so no carry but does overflow

I cant think of any case where you could have carry but NOT overflow

that sounds like Carry is for + and - where Overflow would be / and *

and to think in School I was an expert at this shit :frowning:

well overflow can also happen in + and underflow in -
I dont think they are mutually exclusive
0x8 * 0x2 -> 0x10
which I think would be both carry & overflow

but you’d need a real device to verify this on :slight_smile:

Theoretically, at binary level (0/1), a Carry should be a one bit spill (the carry) you can recover and use, an Overflow should be more than 1 bit, and the result you get may be uncertain due to such “big” loss (as 2 bits :grinning_face_with_smiling_eyes:).

In a 4 bit register :

1111 + 1111 = (1 carry) 1101

// 15+15 or 15 * 2 results the same, 
// MAYBE the firmware may consider 15 * 2 overflow + carry due to the spill not being
// caused by a sum/subtraction. You should observe the behavior being copied

1111 * 11 = (10 overflow) 1101

// 15 * 3

Had to look it up

https://en.wikipedia.org/wiki/Integer_overflow

A real-world problem:

Between 1985 and 1987, arithmetic overflow in the Therac-25 radiation therapy machines, along with a lack of hardware safety controls, caused the death of at least six people from radiation overdoses.[23]

An unhandled arithmetic overflow in the engine steering software was the primary cause of the crash of the 1996 maiden flight of the Ariane 5 rocket.

1 Like

Thanks all… All I needed to indicate was there was a CARRY and/or Overflow condition, which I was able to do… Thanks