Created
April 29, 2014 16:37
-
-
Save scottopell/11405604 to your computer and use it in GitHub Desktop.
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
// The function is supposed to return false when | |
// x+y overflows unsigned short. | |
// Does the function do it correctly? | |
bool IsValidAddition( unsigned short x, | |
unsigned short y) { | |
if (x+y < x) | |
return false; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
During arithmetic operations, the C99 standard states that "if an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int."
Therefore this will never work to check for overflow.