Guest User

Untitled

a guest
Jul 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. if ((int) ((unsigned) a + (unsigned) b) < 0 )
  2. complain();
  3.  
  4. if (a^b < 0) overflow=0; /* opposite signs can't overflow */
  5. else if (a>0) overflow=(b>INT_MAX-a);
  6. else overflow=(b<INT_MIN-a);
  7.  
  8. overflow = (a+b<a);
  9.  
  10. -4,294,967,295 < sum < 4,294,967,295
  11.  
  12. 4,294,967,295 + 1 = -4,294,967,295
  13.  
  14. Do the addition normally, then check the result (e.g. if (a+23<23) overflow).
Add Comment
Please, Sign In to add comment