KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I was writing a program in C++ to find all solutions of a b = c , where a , b and c together use all the digits 0-9 exactly once. The program looped over values of a and b , and it ran a digit-counting routine each time on a , b and a b to check if the digits condition was satisfied. However, spurious solutions can be generated when a b overflows the integer limit. I ended up checking for this using code like: unsigned long b, c, c_test; ... c_test=c*b; // Possible overflow if (c_test/b != c) {/* There has been an overflow*/} else c=c_test; // No overflow Is there a better way of testing for overflow? I know that some chips have an internal flag that is set when overflow occurs, but I've never seen it accessed through C or C++. Beware that signed int overflow is undefined behaviour in C and C++ , and thus you have to detect it without actually causing it. For signed int overflow before addition, see Detecting signed overflow in C/C++ .
Tags (comma-separated)
Save Edits
Cancel