We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2d4a932 commit 7bc13f3Copy full SHA for 7bc13f3
sum-of-two-integers/Jeehay28.js
@@ -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