code_junkie

Checking for overflow of 64-bit integers in C++

Nov 11th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.25 KB | None | 0 0
  1. int64 myint64::add (int64 a, int64 b) {
  2. if (MAX_INT64 - a > b) {
  3. // error condition here.
  4. }
  5. return a + b;
  6. }
  7. int64 myint64::mul (int64 a, int64 b) {
  8. if (MAX_INT64 / a > b) {
  9. // error condition here.
  10. }
  11. return a * b;
  12. }
Add Comment
Please, Sign In to add comment