Skip to content

Commit 7bc13f3

Browse files
committed
Add sum-of-two-integers solution
1 parent 2d4a932 commit 7bc13f3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

sum-of-two-integers/Jeehay28.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} a
3+
* @param {number} b
4+
* @return {number}
5+
*/
6+
7+
// Time Complexity: O(log(max(a, b)))
8+
// Space Complexity: O(1)
9+
10+
var getSum = function (a, b) {
11+
// XOR (^): outputs true (or 1) if the inputs are different, and false (or 0) if the inputs are the same.
12+
// And (&): compares each bit of two numbers and returns 1 only if both bits are 1; otherwise, it returns 0.
13+
// left shitf (<<): moves the bits one position to the left, which is the same as multiplying by 2.
14+
15+
while (b !== 0) {
16+
let carry = (a & b) << 1;
17+
a = a ^ b;
18+
b = carry;
19+
}
20+
21+
return a;
22+
};
23+

0 commit comments

Comments
 (0)