Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if two positive integers will result in an overflow
public boolean willPositiveIntegerAdditionOverflow(int a, int b) {
if (a < 0 || b < 0) {
throw new IllegalArgumentException("this only works with two positive integers");
}
return (a > (Integer.MAX_VALUE - b));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment