Skip to content

Commit cf1c998

Browse files
committed
feat : sum-of-two-integers
1 parent a86efc5 commit cf1c998

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

sum-of-two-integers/ekgns33.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
3+
bit manipulation
4+
5+
find carry of each binary digits sum
6+
example
7+
8+
1 0 1 1
9+
+0 0 1 0
10+
-----------
11+
0 1
12+
^carry 1 (1<<2)
13+
1
14+
1
15+
16+
17+
*/
18+
class Solution {
19+
public int getSum(int a, int b) {
20+
int carryBitSet;
21+
while(b != 0) {
22+
carryBitSet = a & b;
23+
a = a ^ b;
24+
b = carryBitSet << 1;
25+
}
26+
return a;
27+
}
28+
}

0 commit comments

Comments
 (0)