Check for Integer Overflow - GeeksforGeeks

05 Jan, 2022
Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in “result” and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.Method 1 There can be overflow only if signs of two numbers are same, and sign of sum is opposite to the signs of numbers.  
1)  Calculate sum
2)  If both numbers are positive and sum is negative then return -1
     Else 
        If both numbers are negative and sum is positive then return -1
        Else return 0
 
C++
C
#include <bits/stdc++.h>using namespace std; /* Takes pointer to result and two numbers as    arguments. If there is no overflow, the function    places the resultant = sum a+b in “result” and    returns 0, otherwise it returns -1 */int addOvf(int* result, int a, int b){    *result = a + b;    if(a > 0 && b > 0 && *result < 0)        return -1;    if(a < 0 && b < 0 && *result > 0)        return -1;    return 0;} // Driver codeint main(){    int *res = new int[(sizeof(int))];    int x = 2147483640;    int y = 10;     cout<<addOvf(res, x, y);     cout<<"\n"<<*res;    return 0;} // This code is contributed by rathbhupendra
#include<stdio.h>#include<stdlib.h> /* Takes pointer to result and two numbers as    arguments. If there is no overflow, the function    places the resultant = sum a+b in “result” and    returns 0, otherwise it returns -1 */ int addOvf(int* result, int a, int b) {     *result = a + b;     if(a > 0 && b > 0 && *result < 0)         return -1;     if(a < 0 && b < 0 && *result > 0)         return -1;     return 0; }  int main() {     int *res = (int *)malloc(sizeof(int));     int x = 2147483640;     int y = 10;      printf("%d", addOvf(res, x, y));      printf("\n %d", *res);     getchar();     return 0;}
-1
-2147483646
 
Time Complexity: O(1) Space Complexity: O(1)Method 2 Thanks to Himanshu Aggarwal for adding this method. This method doesn’t modify *result if there us an overflow.  
C++
C
#include <bits/stdc++.h>using namespace std; int addOvf(int* result, int a, int b){     if (a >= 0 && b >= 0 && (a > INT_MAX - b)) {        return -1;    }     else if (a < 0 && b < 0 && (a < INT_MIN - b)) {        return -1;    }    else {        *result = a + b;        return 0;    }} int main(){    int a, b;    int* res;    a = INT_MAX;    b = 8192;     cout << addOvf(res, a, b);     return 0;} // This code is contributed by Sonu Kumar Pandit
#include <limits.h>#include <stdio.h>#include <stdlib.h> int addOvf(int* result, int a, int b){    if (a > INT_MAX - b)        return -1;    else {        *result = a + b;        return 0;    }} int main(){    int* res = (int*)malloc(sizeof(int));    int x = 2147483640;    int y = 10;     printf("%d", addOvf(res, x, y));    printf("\n %d", *res);    getchar();    return 0;}
-1
0
 
Time Complexity: O(1) Space Complexity: O(1)  
YouTubeGeeksforGeeks502K subscribersCheck for Integer Overflow | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:47•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=YU4oyORCMdA" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please write comments if you find any bug in above codes/algorithms, or find other ways to solve the same problem 
rathbhupendra
sonukmrpndt
integer-overflow
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Check whether K-th bit is set or not
Program to find parity
Hamming code Implementation in C/C++
Write an Efficient Method to Check if a Number is Multiple of 3
Highest power of 2 less than or equal to given number
Swap bits in a given number
Builtin functions of GCC compiler
Count number of bits to be flipped to convert A to B
Reverse actual bits of the given number