-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_74.c
25 lines (22 loc) · 808 Bytes
/
2_74.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <limits.h>
/* Determine whether arguments can be subtracted without overflow
* 跟2.73类似
*/
int tsub_ok(int x, int y) {
int a = x;
int b = ~y + 1;
int sum = a + b;
int positive_overflow = !(a & INT_MIN) && !(b & INT_MIN) && (sum & INT_MIN);
int negative_overflow = (a & INT_MIN) && (b & INT_MIN) && !(sum & INT_MIN);
return !positive_overflow && !negative_overflow;
}
int main(void)
{
printf("tsub_ok(%d, %d): %d\n", 1, 1024, tsub_ok(1, 1024));
printf("tsub_ok(%d, %d): %d\n", INT_MIN, 1, tsub_ok(INT_MIN, 1));
printf("tsub_ok(%d, %d): %d\n", INT_MIN, INT_MAX, tsub_ok(INT_MIN, INT_MAX));
printf("tsub_ok(%d, %d): %d\n", INT_MAX, -1, tsub_ok(INT_MAX, -1));
printf("tsub_ok(%d, %d): %d\n", INT_MAX, INT_MAX, tsub_ok(INT_MAX, INT_MAX));
return 0;
}