Given unsigned int a, b
, this expression can never evaluate to true
:
(a+b) > UINT_MAX
The reason why it can't ever be true
is because UINT_MAX
is the max, so whatever result (a+b)
is can never be greater than UINT_MAX
, because that would be a contradiction.
You tried to use a bigger data type long
to get around this, but this is actually not guaranteed to be bigger than int
in C++. You may want to try long long
instead (although this type does not exist in standard C++).
Unlike, say, Java, C++ does not actually specify how large each data type must be, so the claim that UINT_MAX=65535
is not universally true. It's only true for processors where an unsigned integer is held in 16 bits. For 32 bits, UINT_MAX
is 4294967295
.
Related questions
- Best way to detect integer overflow in C/C++
See also
- Wikipedia/Limits.h
- cplusplus.com/reference/library/climits