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 80ec484 commit 9716591Copy full SHA for 9716591
sum-of-two-integers/byol-han.js
@@ -0,0 +1,22 @@
1
+/**
2
+ * https://leetcode.com/problems/sum-of-two-integers/submissions/1649575939/
3
+ * @param {number} a
4
+ * @param {number} b
5
+ * @return {number}
6
+ */
7
+var getSum = function (a, b) {
8
+ while (b !== 0) {
9
+ // a와 b의 합에서 자리올림(carry)을 제외한 값 계산 (XOR 연산)
10
+ let sum = a ^ b;
11
+
12
+ // 자리올림(carry)을 계산 (AND 연산 후 왼쪽으로 한 비트 이동)
13
+ let carry = (a & b) << 1;
14
15
+ // 새로운 a는 sum, 새로운 b는 carry가 됨
16
+ a = sum;
17
+ b = carry;
18
+ }
19
20
+ // carry가 0이 되면 더할 게 없으므로 최종 결과 a 반환
21
+ return a;
22
+};
0 commit comments