# Overflow Detection

#### vincent19-mas

Joined Dec 27, 2012
83
Dear friends,

I read about overflow detection. But one thing that I do not understand is that what is the purpose of classifying the arithmetic operation to either overflow exist or not ?

Please have a look at the image attached

If overflow means that the output is invalid ?

For example, +5 + 3 = +8 (expected)

but in actual it gave us 1000, which is -8 in 2s complement. So means the operation is not valid ?

Thanks !

#### Attachments

• 25.4 KB Views: 28
• 15.7 KB Views: 29

#### Papabravo

Joined Feb 24, 2006
14,678
In a 2's complement representation an overflow is the result of arithmetic operations exceeding the upper or lower bounds of the representation range. In your example a 4-bit quantity can represent any number in the range [-8,...,+7]. Any result outside that range is an overflow. If you look at the datasheet for a processor with an overflow condition code, you will see a Boolean equation for overflow. IIRC it involves the sign of the result and the signs of the operands.

#### vincent19-mas

Joined Dec 27, 2012
83
In a 2's complement representation an overflow is the result of arithmetic operations exceeding the upper or lower bounds of the representation range. In your example a 4-bit quantity can represent any number in the range [-8,...,+7]. Any result outside that range is an overflow. If you look at the datasheet for a processor with an overflow condition code, you will see a Boolean equation for overflow. IIRC it involves the sign of the result and the signs of the operands.
So does my example shown is overflow case ? why is that so ?

Thanks !

#### shteii01

Joined Feb 19, 2010
4,644
So does my example shown is overflow case ? why is that so ?

Thanks !
You don't have an example.

Do you have unsigned 4 bit number?
Do you have signed 4 bit number where 3 bits represent the number and fourth bit represent sign?

#### Papabravo

Joined Feb 24, 2006
14,678
You have to pick a representation. The 4-bit quantities are either unsigned or signed 2's complement or they could be signed 1s complement or any other variety of representation. Once we know what is the intended representation we can tell what "overflow" means.

In your first example you have an "expectation" that you can represent +8 as the sum of +3 and +5. This is nonsense since +8 is not a representable number in 2's complement signed notation.

#### WBahn

Joined Mar 31, 2012
26,301
Dear friends,

I read about overflow detection. But one thing that I do not understand is that what is the purpose of classifying the arithmetic operation to either overflow exist or not ?

Please have a look at the image attached

If overflow means that the output is invalid ?

For example, +5 + 3 = +8 (expected)

but in actual it gave us 1000, which is -8 in 2s complement. So means the operation is not valid ?

Thanks !
Using 4-bit 2-complement representation, 5 + 3 = -8, right?

If that a correct result (in terms of normal arithmetic)?

If the answer is no, then an overflow has occured, otherwise an overlow hasn't occured.

It's really that simple.

#### vincent19-mas

Joined Dec 27, 2012
83
Using 4-bit 2-complement representation, 5 + 3 = -8, right?

If that a correct result (in terms of normal arithmetic)?

If the answer is no, then an overflow has occured, otherwise an overlow hasn't occured.

It's really that simple.
If overflow occurs, means we do not need to care for the result or the output ? We can ignore ?

Thanks !

#### shteii01

Joined Feb 19, 2010
4,644
If overflow occurs, means we do not need to care for the result or the output ? We can ignore ?

Thanks !
If overflow occurs, then the number that you just calculated will mess up some other future calculation, which in turn can result in nothing or a nuclear disaster if the system that had the overflow is part of nuclear reactor control system.

You are the designer. If overflow is ok with you, then it is ok with us. If it kills you, we don't care.

#### vincent19-mas

Joined Dec 27, 2012
83
If overflow occurs, then the number that you just calculated will mess up some other future calculation, which in turn can result in nothing or a nuclear disaster if the system that had the overflow is part of nuclear reactor control system.

You are the designer. If overflow is ok with you, then it is ok with us. If it kills you, we don't care.
Okay!

Noted and thanks !

So it is better to ignore the output that overflow

#### MrChips

Joined Oct 2, 2009
22,092
How did you arrive at that conclusion?

If an overflow occurred and the result is incorrect then you have a problem.

#### vincent19-mas

Joined Dec 27, 2012
83
How did you arrive at that conclusion?

If an overflow occurred and the result is incorrect then you have a problem.
In my case of study which I am currently learning digital electronics theory. So we are taught to ignore the output if overflows happen and we are not taught on how to fix that yet.

Thanks

#### WBahn

Joined Mar 31, 2012
26,301
If overflow occurs, means we do not need to care for the result or the output ? We can ignore ?

Thanks !
What do you mean by "ignore"? Just pretend nothing happened and proceed on with the calculations?

What is the most appropriate thing to do depends on the application in question and what the consequences are of the various options.

#### vincent19-mas

Joined Dec 27, 2012
83
What do you mean by "ignore"? Just pretend nothing happened and proceed on with the calculations?

What is the most appropriate thing to do depends on the application in question and what the consequences are of the various options.
I mean just stated there output is invalid.

Thanks !

#### MrChips

Joined Oct 2, 2009
22,092
No. There can be serious consequences depending on the application if an overflow occurs.

One approach is to ensure that an overflow never occurs.

#### WBahn

Joined Mar 31, 2012
26,301
I mean just stated there output is invalid.

Thanks !
Okay, so output is invalid. What is your system going to do? Issue an error message? Lock up? Use a default value? What?