-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq17.1.cpp
More file actions
66 lines (54 loc) · 1.21 KB
/
q17.1.cpp
File metadata and controls
66 lines (54 loc) · 1.21 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;
int shiftbits(int num, int count, bool dir) {
if (dir) {
for (int it = 0; it < count; it++) {
num = num >> 1;
}
}
else {
for (int it = 0; it < count; it++) {
num = num << 1;
}
}
return num;
}
int sum_helper(int a, int b, bool carry) {
int sum = 0;
int count = 0;
int f = 0;
bool b1, b2;
while ((a != 0) && (b != 0)) {
b1 = (a & 0x01);
b2 = (b & 0x01);
f = b1 ^ b2 ^ carry;
sum |= shiftbits(f, count, 0);
carry = (b1 & b2) | ((b1 | b2) & carry);
a = shiftbits(a, 1, 1);
b = shiftbits(b, 1, 1);
count++;
}
return sum;
}
int sum(int a, int b) {
// int sum = 0;
// int sign_a = a < 0 ? -1 : 1;
// int sign_b = b < 0 ? -1 : 1;
return sum_helper(a, b, 0);
}
// int add ( int a, int b) {
// while (b != 0) {
// int sum = a ^ b;
// int carry = (a & b) << 1;
// a = sum;
// b = carry;
// cout << a << ", " << b << endl;
// }
// return a;
// }
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
int a = 29, b = 14;
int ans = sum(a, b);
cout << " sum of " << a << " and " << b << " : " << ans;
return 0;
}