Skip to content

Commit 05a6e0c

Browse files
committed
Sum of Two Integers solution
1 parent 5cd52d6 commit 05a6e0c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

sum-of-two-integers/PDKhan.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int getSum(int a, int b) {
4+
unsigned int result = 0;
5+
int carry = 0;
6+
7+
for(int i = 0; i < 32; i++){
8+
unsigned int aa = a & (1U << i);
9+
unsigned int bb = b & (1U << i);
10+
11+
if(aa && bb){
12+
if(carry)
13+
result |= (1U << i);
14+
15+
carry = 1;
16+
}else if(aa == 0 && bb == 0){
17+
if(carry)
18+
result |= (1U << i);
19+
20+
carry = 0;
21+
}else{
22+
if(carry == 0)
23+
result |= (1U << i);
24+
}
25+
}
26+
27+
return result;
28+
}
29+
};

0 commit comments

Comments
 (0)