Skip to content

Commit 4aa4aa4

Browse files
committed
add: solve #284 Sum of Two Integers with ts
1 parent f657baf commit 4aa4aa4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

sum-of-two-integers/Yjason-K.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* +, - 기호를 사용하지 않고 두 정수의 합을 구하기
3+
* @param {number} a - 정수
4+
* @param {number} b - 정수
5+
* @return {number} - 두 정수의 합
6+
*/
7+
function getSum(a: number, b: number): number {
8+
// 1. XOR 연산으로 덧셈 처리
9+
// 2. AND 연산으로 올림 처리
10+
// 3. 올림이 없을 때까지 반복
11+
while (b !== 0) {
12+
const carry = a & b;
13+
a = a ^ b;
14+
b = carry << 1;
15+
}
16+
return a;
17+
}
18+

0 commit comments

Comments
 (0)