The following integer overflow detection function is given in Computer Systems: A Programmer’s Perspective, 3e.
/* Determine whether arguments can be added without overflow */
int tadd_ok(int x, int y) {
int sum = x+y;
int neg_over = x < 0 && y < 0 && sum >= 0;
int pos_over = x >= 0 && y >= 0 && sum < 0;
return !neg_over && !pos_over;
}
The expression given for negative overflow matches the one given in the book. The expression for positive overflow seems to differ slightly. In the book, it says positive overflow can be detected if x > 0, y > 0, and sum <= 0. I’m wondering why the expression pos_over in the C function is different from this.
Thank you!
New contributor
ququ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.