What is Overflow in case of Binary Arithmetic

Many a time, overflow in a binary addition seems like a final carry generation. However, overflow leads to an incorrect sum, whereas carry does not. This is a little confusing if not understood correctly. So, let’s understand the overflow concept clearly.

Overflow

Imagine you have a half-tea-filled cup. Your friend has a jar with some amount of tea in it. He wants to measure the volume of total tea you and he has. So he pours his tea into your cup. However, the volume of the total amount of tea is more than the size of your cup. So, the tea started spilling out, when your friend was pouring his tea into your cup. The important thing to notice here is that it will overflow if you pour more than the capacity of the container. This concept is similar to the overflow in binary addition.

Let’s take an example to understand this.

Example of an overflow in a binary addition

In the above example, we are performing the addition 120 + 62. The result should be 182 as shown in the above picture. However, it can be seen that the binary addition did not result in the correct sum. Now, let’s understand where exactly the things went wrong, though we have followed the basic rules of binary arithmetic.

We have adopted 2’s complement 8-bit representation of numbers. Both the numbers taken (120, 62) can be represented using 7 magnitude bits, while leaving one bit for sign (‘0’ for both the numbers). However, the result (182 => 10110110) requires 8 bits for its magnitude. Since it is a 2’s complement 8-bit representation, the 8th bit of the sum is considered as the sign bit. The sign bit in the sum is ‘1’, so is considered as a negative number. Moreover, the magnitude of the sum is also incorrect. The generated sum is now -74, whereas the correct answer is 182. Why this happened?

This happened because 182 is not within the range of signed numbers that can be represented by 8 binary bits. The generated sum, that is 182, requires 9 bits to be represented correctly in 2’s complement representation. If we start with two n-bit numbers and the sum occupies n+1 bits, then an overflow results as indicated by an incorrect sign bit. Overflow is a problem in computers because the number of bits that hold a number is finite, and a result that exceeds the finite value by 1 can not be accommodated.

Important Fact: An overflow can occur only when both numbers are positive or both numbers are negative. If the sign bit of the result is different than the sign bit of the numbers that are added, overflow is indicated.

How to avoid Overflow: Add another ‘0’ to a positive number and another ‘1’ to a negative number in the most significant position to extend them to n+1 bits and then perform the addition. In order to obtain a correct answer, we must ensure that the result has a sufficient number of bits to accommodate the sum.

How to avoid Overflow in the above Tea Spilling Problem: To measure the total amount of tea, you should pour your tea into the jar of your friend. Since, the jar is of larger size, it could accommodate all the tea.

Overflow vs. Final Carry Out

Overflow and carry out are philosophically the same thing. Both indicate that the answer does not fit in the space available. Carry out is generated, when there is a carry out of the most-significant bit. The overflow happens when there is a carry into the most significant bit.

Carry Out: It can be observed in the case of unsigned number arithmetic. However, with the generation of carry out, the sum does not get corrupted. The carry flag is set. The content of the register holding the result along with the carry provides the correct sum.

Overflow: It can be observed in the case of signed number arithmetic. However, with the generation of overflow, the sum gets corrupted. The overflow flag is set, which indicates the result is incorrect.

You can find more examples on detection and avoidance of overflow in the next article.

Previous           Table of Content           Next

Leave a Reply

Your email address will not be published. Required fields are marked *