Table of Contents

# Overflow of Numbers

How to Determine the Overflow of Signed and Unsigned Numbers?. Numbers represented in computer system have always a specific range. It depends on how many bits your system can process. Some systems are 32 bits while other are 64-bits which means they can transfer or receive either 32 bits or 64 bits at a time respectively. With the advancement in technology we have now** l**arger 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 property is known as **underflow and it is the opposite phenomenon of overflow. **

## Unsigned Integers Representation

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 stored 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 for calculating the maximum number representation for given n number of bits is 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 n number of bits.

## How to Decide if the Given bits Represent an Even or an Odd number?

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. **

Also read about

#### How to design a four bit adder-subtractor circuit?

**Signed Integers Representation in Computer System**

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.

## How to 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.

#### Also check

### What is the machine epsilon?

## Overflow of Negative Numbers Example

Consider we want to store -3 in computer

first find its binary combination in 8 bits that is

0000 0011

take one’s compliment (For this invert all bits) so

1’s compliment is: 1111 1100

and then add 1 in it.

1111 1100

+1

_________________

1111 1101

_________________

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

2^8=256

2^8/2=128

128-1=127

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

### Example

1111 1111 1111 1111

+0000 0000 0000 0001

____________________

1 0000 0000 0000 0000

____________________

### Unsigned Interpretation for Overflow Check

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. **

### Signed Interpretation

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. **

### Example of Overflow Check

0111 1111 1111 1111

0111 1111 1111 1111 +

_________________

1111 1111 1111 1110

_________________

### Unsigned Interpretation

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.

### Signed Interpretation

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).

## What is Unsigned Overflow?

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.

## What is Signed Overflow?

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.

### Easy way to determine the overflow

There is an easy way to understand either there is overflow or not. If there is a carry in into the MSB and a carry out from the MSB then there will be no overflow. But if there a carry in into the MSB but not a carry out or if there is a carry out from the MSB but not a carry in then the overflow occurs.

### Unsigned Overflow Identification (easiest way)

On addition, unsigned overflow occurs when there Is a carry out of the msb. This means that the correct answer is larger than the biggest .unsigned number; that is, FFFl’h for a word and FFh for a byte .• on subtraction, unsigned overflow occurs when there is a borrow into the msb. This means that the correct answer is smaller than 0.

### Signed Overflow Identification (easiest way)

On addition of numbers with the same sign, signed overflow occurs when the sum has a different sign. This happened in the preceding example when we were adding 7FFFh. and 7FFFh (two positive numbers), but got FFFEh .(a negative result), · Subtraction of numbers with different signs ls like adding numbers of the same sign. For example, A – ( -B) = A + B and -A -(+B) =-A + -B. Signed overflow occurs if the result has a different sign than expected.

In addition of numbers With different signs, overflow Is impossible, because a sum like A + ( -B) is really A – B, and because A and B are small enough to fit in the destination, so is A – B. For exactly the same reason, subtraction of numbers with the same sign cannot give· overflow.

## Conclusion

The concept of overflow is important in the context of storing or representing the numbers. Because the misinterpretation of the result can be done easily if you are not well familiar about it.

## Python Code for Determining the Overflow

Here is the code in Python for testing the overflow.

def check_overflow_addition(a, b, max_value):

result = a + b

if result > max_value:

return True

else:

return False

# Example usage for 8-bit unsigned integers

a = 200

b = 100

max_value_8bit = 255

overflow = check_overflow_addition(a, b, max_value_8bit)

print(f”Overflow: {overflow}”)

## C Code for Determining the Overflow

#include <stdio.h>

#include <stdbool.h>

bool check_overflow_addition(int a, int b, int max_value) {

int result = a + b;

if (result > max_value) {

return true;

} else {

return false;

}

}

int main() {

// Example usage for 8-bit unsigned integers

int a = 200;

int b = 100;

int max_value_8bit = 255;

bool overflow = check_overflow_addition(a, b, max_value_8bit);

printf(“Overflow: %s\n”, overflow ? “true” : “false”);

return 0;

}

### Quiz for Testing your Knowledge of Overflow

Test Your Knowledge Overflow of numbers

watch here for video lecture

**Related Topics **

- What are number systems in computer?
- Number System Conversions – Briefly Explained
- How to design Half adder and Full adder circuits?
- What are the decoders? how are they different from encoders?
- What is binary arithmetic? How to add, subtract, multiply and divide two binary numbers?
- What is the magnitude comparator circuit? Design a 3 bit magnitude comparator circuit
- How to design a 4 bit magnitude comparator circuit?
- How many hundreds are there in 1000?
- What are the synchronous counters? Explain with an example
- what are the BCD and binary ripple counters?

## One thought on “How to Determine the Overflow of Signed and Unsigned Numbers?”