Introduction to overflow of numbers
How to define and determine overflow of signed and unsigned numbers? Why it is necessary to get the idea of overflow? Numbers represented in computer system have always have a specific range. It depends on how many bits your system process. Some systems are 32 bits while other are 64-bits. With the advancement in technology we have now larger bits processing systems. But they have limited range of numbers they can represent. Overflow refers to the condition when the given bits can not represent the given number. It means that number needs more bits for its representation. There is another situation where the number is small enough and not representable according to the given pattern of numbers. This is called underflow.
How to define and determine overflow of unsigned numbers? Unsigned integers are those integers that are used for representing the magnitudes. They do not need any sign for their representation because these quantities are always positive. Such quantities are used for representing the quantities such as addresses of memory locations, and ASCII characters codes.
The largest value that can be stores in 8-bits is (11111111) FFH that is 255 in decimal. Similarly in a word this value becomes (FFFFH) or 65535. Now the question arises how these values are determined. The general formula is to calculate (2^n)-1. Where n is the given number of bits for which you want to determine the range. For example, for 8 bits we have 2^8-1= 256-1=255. 1 is subtracted because 0 is included in the range. So there are total 256 numbers and the range for 8-bit numbers is 0 to 255.
The same formula can be extended for any number of bits. If the least significant bit (LSB) of a binary combination is zero then the number is even and if it is one (1) then the number is odd.
How to define and determine overflow of signed numbers? Unsigned Integers are little bit tricky to understand. All we need is little bit more attention for understanding them. Whenever a number is treated as signed then it be either positive or negative. In binary representation, the most significant bit (MSB) is reserved for representing the sign. If MSB=0 then the number is positive and if MSB=1, then the number is negative.
Determining the range of signed numbers
In order to determine the range of signed numbers consider the following example of 8 bit numbers
For 8 bit numbers, msb is reserved for sign so all bits will be equal to 1( 01111111) while representing the largest positive number. The weighted binary sum of this combination gives
The smallest negative number is 1000 0000 means all other bits are equal to zero and only sign bit is set high.
How negative numbers are stored?
The negative numbers are usually stored as 2’s compliment or by taking the one’s compliment and then adding 1 in it.
Consider we want to store -3 in computer
first find its binary combination in 8 bits that is
take one’s compliment (For this invert all bits) so
1’s compliment is: 1111 1100
and then add 1 in it.
so in this way any number can be stored.
The range of n bit signed numbers is determines as (2^n)/2 -1
In case of 8-bit numbers
so the numbers lie in between -128 to 127. If a number that has value out of this range then it will cause overflow. E.g., if there is an addition of two numbers that fall within the range. Now their resultant is some number that does not lie within this range then it will show overflow. Status flag is used for representing the overflow.
There can be both types of overflow, signed overflow and unsigned overflow. It depends how the numbers are treated.
Consider the following example again
1111 1111 1111 1111
+0000 0000 0000 0001
1 0000 0000 0000 0000
if both numbers are treated as unsigned numbers , then the first combination gives (65535) and second combination is equal to 1. Adding both of them will result 65536 that is out of range of unsigned integers (as 65535 is the largest possible value). Here unsigned overflow occurred.
If both numbers are treated as signed then the first number (1111 1111 1111 1111) is equal to -1
and the second number is (+1)
so if we add (-1)+(1) then the resultant is equal to zero which is true as it matches with the result (1 0000 0000 0000 0000). So no signed overflow occurred.
0111 1111 1111 1111
0111 1111 1111 1111 +
1111 1111 1111 1110
If the above number is treated as unsigned then its value is (32767). As both numbers are same so when we add them we get, 131068 which falls within the range of unsigned integers. So, no unsigned overflow occurred.
If we treat both of them as signed then both are positive numbers as msb=0. But the resultant has msb =1 which shows some negative number. Although the addition of two positive numbers can never be a negative one so signed overflow occurred. In terms of magnitude the resultant is 131068 that exceeds the value of positive numbers of 16-bit combination (32767).
From above examples it is concluded that signed overflow occurs during addition when there is a carry out of MSB. Which means the represented number is greater than the available bits or biggest numbers FFH for 8-bits and FFFFH for 16 bits. During subtraction the unsigned overflow occurs when there is a borrow into the MSB. In this case the correct answer is actually smaller than 0.
During addition, if both numbers that you are adding are positive but the resultant is negative, then signed overflow occurs. When numbers are of different signs during addition, then signed overflow is impossible. As A-B or -A+B will always be smaller than the available bits. While in subtraction, if the resultant has different sign as expected then it will cause overflow.
How overflow is indicated by the processor in computer?
Two flags are used for indicating the unsigned overflow (Carry flag CF=1) and (overflow flag OF=1) for signed overflow. These two flags are present in status register of computer.
If you guys have more questions then you are more welcome!!!
watch here for video lecture
Also read here