-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint_aplusb.cpp
44 lines (36 loc) · 1.01 KB
/
lint_aplusb.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
/*************************************************************************
> File Name: lint_bit_aplusb.cpp
> Author: Mengqi Wang
> Created Time: Sun Nov 15 20:04:57 2015
> adding two 32-bit int w/o using "+" operand
************************************************************************/
#include <iostream>
using namespace std;
class Solution {
public:
/*
* @param a: The first integer
* @param b: The second integer
* @return: The sum of a and b
*/
int aplusb(int a, int b) {
// try to do it without arithmetic operators.
int sum = a;
int carry = b;
while (carry) {
int tmp = sum;
// add w/o carry
sum = tmp ^ carry;
// record carry on i-th bit, add it into i-th bit
carry = (tmp & carry) << 1;
}
return sum;
}
};
int main() {
int a = 100000000;
int b = 200000000;
Solution *s = new Solution;
cout << s->aplusb(a, b) << endl;
return 0;
}