Created
December 2, 2017 20:00
-
-
Save danielwagn3r/d52ad965f2994b8a031f662cfdef7f2f to your computer and use it in GitHub Desktop.
Overflow Check in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
} | |
if ((b > 0 && a <= INT_MAX / b && a >= INT_MIN / b) || | |
(b == 0) || | |
(b == -1 && a >= -INT_MAX) || | |
(b < -1 && a >= INT_MAX / b && a <= INT_MIN / b)) | |
{ | |
result = a * b; | |
} | |
else | |
{ | |
/* calculation would overflow */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment