How to detect integer overflow in C++

In this tute, we will discuss how to detect integer overflow in C++. For this, let us try to understand how integers are stored.

If you know the basics of Integers, you can straight away go for the methods. If you are an absolute beginner, this little section is for you…

In 64-bit compilers, Integers use 4 bytes / 32 bits of data which is split into 1 bit for the sign(+/-) and remaining 31 bits for value. 231-1 = 2147483647.

Here is a quick representation of what happens with integers in C++,

…, 2147483647, -2147483648, -2147483647, .., -2. -1. 0, 1, 2, … 2147483646, 2147483647, -2147483648, …

This cycle goes on and once you reach the max. of one sign, it switches to min. of other sign and vice-versa.

Method 1: Using simple sign concept

Let us consider 3 scenarios:

  • Both integers are +ve. The sum of the two will always be bigger.
  • Both integers are -ve. Sum of the two will always be smaller.
  • One +ve and other -ve. The sum would neither be greater than the +ve number nor smaller than the -ve number.

We know that the integer value lies between -2147483648 and 2147483647.

In the first case, if the sum crosses 2147483647, it goes to the -ve part (Overflow). In the second case, if the sum crosses -2147483648, it goes to the +ve part (Overflow).

#include<bits/stdc++.h>
using namespace std;

/* Check if adding x and y results in overflow.
   If overflow, return true(1).
   Otherwise, store the sum in res and return false(0) */
bool addOverflow(int x, int y, int &res)
{
    int temp = x + y;
    if(x>0 && y>0 && temp<0)
        return 1;
    if(x<0 && y<0 && temp>0)
        return 1;
    
    res = x + y;
    return 0;
}

int main()
{
    int x, y, res = 0;

    x = 45; y = 2147483647;
    cout<<"Overflow(0/1): "<<addOverflow(x, y, res)<<endl;
    cout<<"Result: "<<res<<endl;

    x = 45; y = 10;
    cout<<"Overflow(0/1): "<<addOverflow(x, y, res)<<endl;
    cout<<"Result: "<<res<<endl;

    return 0;
}

Output:

Overflow(0/1): 1
Result: 0
Overflow(0/1): 0
Result: 55

Time Complexity: O(1)

Space Complexity: O(1)

Method 2: Using long integers

In this method, we’ll use long integers to check for integer overflow. Since long integers have a bigger capacity, the sum of two integers would definitely fit into them. Hence, we can use an auxiliary long integer to catch the overflow.

#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;   // To use ll instad of long long int

/* Check if adding x and y results in overflow.
   If overflow, return true(1).
   Otherwise, store the sum in res and return false(0) */
bool addOverflow(int x, int y, int &res)
{
    ll tmp1 = ll(x) + ll(y);
    int tmp2 = x + y;

    if(tmp1 != tmp2)
        return 1;
    
    res = x + y;
    return 0;
}

int main()
{
    int x, y, res = 0;

    x = 45; y = 2147483647;
    cout<<"Overflow(0/1): "<<addOverflow(x, y, res)<<endl;
    cout<<"Result: "<<res<<endl;

    x = 45; y = 10;
    cout<<"Overflow(0/1): "<<addOverflow(x, y, res)<<endl;
    cout<<"Result: "<<res<<endl;

    return 0;
}

Output:

Overflow(0/1): 1
Result: 0
Overflow(0/1): 0
Result: 55

Time Complexity: O(1)

Space Complexity: O(1)

Method 3: Using boundary values

Since we know the boundary values of integer, we can use them as a reference to detect integer overflow in C++.

#include<bits/stdc++.h>
using namespace std;

/* Check if adding x and y results in overflow.
   If overflow, return true(1).
   Otherwise, store the sum in res and return false(0) */
bool addOverflow(int x, int y, int &res)
{
    if(x>0 && y>0 && (x > INT_MAX - y))
        return 1;
    if(x<0 && y<0 && (x < INT_MIN - y))
        return 1;
    
    res = x + y;
    return 0;
}

int main()
{
    int x, y, res = 0;

    x = -45; y = -2147483647;
    cout<<"Overflow(0/1): "<<addOverflow(x, y, res)<<endl;
    cout<<"Result: "<<res<<endl;

    x = 45; y = 2147483647;
    cout<<"Overflow(0/1): "<<addOverflow(x, y, res)<<endl;
    cout<<"Result: "<<res<<endl;

    return 0;
}

Output:

Overflow(0/1): 1
Result: 0
Overflow(0/1): 1
Result: 0

Time Complexity: O(1)

Space Complexity: O(1)

To know more about C++ datatypes and their ranges:

Leave a Reply

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