Overflow in Arithmetic Addition in Binary Number System
In Computer Architecture 2’s Complement format is widely used. The discussion of overflow here mainly will be with respect to 2’s Complement representation for signed Integer. Overflow Occurs with respect to addition when 2 N-bit 2’s Complement Numbers are added and the answer is too large to fit into that N-bit Group.
A computer has N-Bit Fixed registers. Addition of two N-Bit Number will result in a max N+1 Bit number. That Extra Bit is stored in the carry Flag. But Carry does not always indicate overflow.
What is Binary Number System?
The binary number system is a base-2 numeral system that uses two symbols: Zero and one respectively. This system serves as the fundamentals of all the binary codes in use within the computer systems, especially in processing and storage of data. In this system that each binary digit (bit) stands for an increasing power of 2 from right to the left. Database is crucial in any computer system for storing and processing of information and this is common in addition processes using the 2’s complement binary arithmetic.
Conditions of Overflow
Overflow takes place when the result of the calculations exceeds the range of the number to be represented in a fixed number of bits with the help of 2’s complement format. Specifically, overflow happens under the following conditions:
1) Addition of Two Positives
When adding two positive numbers and the amount is more than the maximum amount that an N-bit system can hold, then over flow takes place. Incase it becomes the most significant bit is set to one signifying that the result is negative and this is wrong.
2) Addition of two negative number
Overflow is obtained in the subtraction, when two negative numbers are summed up and with the result, which is placed below the minimum representable value. Here the MSB becomes 0 which means positive result which is also wrong.
3) Variety of Carry in and Carry Out bits
This form of overflow is identified by comparing the carry-in and the carry-out relating to the MSB in an addition. However, if the two bits are at different state, that is C-in is not equal to C-out, then an overflow has taken place. This condition enables a determination of when the result cannot be represented using the available N–bit space.
N-bit 2’s Complement representation for signed Integer can represent numbers from to
4 Bit can represent numbers from ( -8 to 7 )
5 Bit can represent numbers from ( -16 to 15 ) in 2’s Complimentary System.
Adding 7 + 1 in 4-Bit must be equal to 8. But 8 cannot be represented with 4 bit 2’s complement number as it is out of range. Two Positive numbers were added and the answer we got is negative (-8). Here Carry is also 0. It is normally left to the programmer to detect overflow and deal with this situation.
Overflow Detection
Overflow occurs when:
- Two negative numbers are added and an answer comes positive or
- Two positive numbers are added and an answer comes as negative.
Let us understand more in-depth. Consider the below scenarios,
- If x & y are +ve numbers then x+y > max(x,y)
- if x & y are -ve numbers then x+y < min(x,y)
- If x is +ve and y is -ve then y< x+y < x
Considering the above scenarios we can also say that – If we add two operands of same sign and get result of opposite sign, overflow occurs.
So overflow can be detected by checking Most Significant Bit(MSB) of two operands and answer. But Instead of using a 3-bit Comparator, Overflow can also be detected using 2 Bit Comparator just by checking Carry-in(C-in) and Carry-Out(C-out) from MSB’s. Consider the N-Bit Addition of 2’s Complement number.
Overflow Occurs when C-in C-out. The above expression for overflow can be explained below Analysis.
- In first Figure the MSB of two numbers are 0 which means they are positive. Here if C-in is 1 we get answer’s MSB as 1 means answer is negative (Overflow) and C-out as 0. C-in C-out hence overflow.
- In second Figure the MSB of two numbers are 1 which means they are negative. Here if C-in is 0 we get answer MSB as 0 means answer is positive(Overflow) and C-out as 1. C-in C-out hence overflow.
Hence, from above discussion we can also conclude that – If carry for MSB(c-in) and carry from MSB(c-out) is different ( considering first figure 1 and 0 respectively) then overflow occurs. Else there is no overflow.
Readers can also try out other combinations of c-in(carry for MSB) c-out(carry from MSB) and MSB’s to check overflow. So Carry-in and Carry-out at MSB’s are enough to detect Overflow.
The above XOR Gate can be used to detect overflow.
Conclusion
This article has described the overflow conditions occurring in 2’s complement representation of signed integers. It explained the survey of binary number formation and its usage in computer architecture and defined the event of overflow when the outcome of addition exceeds N numbers. A brief explanation of overflow detection methods was provided, touching on the fact that one must compare the carry-in and carry-out bits at the most significant bit in order to pinpoint when overflow occurs. Thus, knowing these concepts helps programmers to address issues with overflow and provide mathematically correct calculations in computer systems.
Frequently Asked Questions on Overflow in Arithmetic Addition in Binary Number System- FAQs
What is it that sign extension in 2’s complement, performs when used in computation?
Sign extension makes sure that when a number is converted to a bigger bit size the sign bit of the number takes the new higher order bits.
In what way does the use of 2’s complement help to simplify the process with negative number representation?
One of the drawbacks of negative number representation is eliminated by 2’s complement where addition for positive numbers is different from that of negative numbers.
Does overflow affect the results of signed integer comparisons?
Yes, overflow is one of the issues responsible for distorting sign integer comparison and makes the resultant comparison if not performed accordingly to be wrong.