forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.js
More file actions
30 lines (28 loc) · 590 Bytes
/
res.js
File metadata and controls
30 lines (28 loc) · 590 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Reference: http://www.cnblogs.com/kiven-code/archive/2012/09/15/2686922.html
*
* res.js
* @authors Joe Jiang ([email protected])
* @date 2017-03-03 22:05:30
*
* Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
*
* Example
*
* Given a = 1 and b = 2, return 3.
*
* @param {number} a
* @param {number} b
* @return {number}
*/
let getSum = function(a, b) {
if (b === 0) {
return a;
}
while (b !== 0) {
let t = a^b;
b = (a&b) << 1;
a = t;
}
return a;
};