Skip to content

Commit d1c6d4d

Browse files
committed
Exercise 2.1
1 parent 83760b8 commit d1c6d4d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

exercise-2.1.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <cassert>
2+
#include <iostream>
3+
#include "my_intrinsics.h"
4+
#include "my_type_functions.h"
5+
6+
using namespace std;
7+
8+
bool p(int a, int b) {
9+
if (a < 0) return b >= INT_MIN - a;
10+
if (a > 0) return b <= INT_MAX - a;
11+
return true;
12+
}
13+
14+
bool p2(int a, int b) {
15+
assert(sizeof(long long) > sizeof(int));
16+
long long sum = (long long) a + b;
17+
return sum >= INT_MIN && sum <= INT_MAX;
18+
}
19+
20+
int main() {
21+
cout << boolalpha;
22+
cout << p2(INT_MIN, -1) << endl;
23+
cout << p2(INT_MAX, 1) << endl;
24+
cout << p2(INT_MIN / 2, INT_MIN / 2) << endl;
25+
cout << p2(INT_MAX / 2, INT_MAX / 2) << endl;
26+
cout << p2(INT_MAX / 2 + 1, INT_MAX / 2 + 1) << endl;
27+
}

0 commit comments

Comments
 (0)