# LeetCode : 0371. Sum of Two Integers (bits)
###### tags:`leetcode`
```
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <bitset>
using std::vector;
using std::unordered_map;
using namespace std;
class BaseVectorPrint {
public:
void BasePrint00(vector<int>& num) {
for (size_t i = 0; i < num.size(); ++i) {
cout << "[" <<num.at(i) << "]" << "; ";
}
cout << endl;
}
void BasePrint01(vector<int>& num) {
for (size_t i = 0; i < num.size(); ++i) {
cout << num[i] << "; ";
}
cout << endl;
}
void BasePrint02(vector<int>& num) {
for (const auto &item : num) {
cout << item << "; ";
}
cout << endl;
}
void TwoDimensionalPrint(vector<vector<int>> &num){
for (int i = 0; i < num.size(); i++)
{
for (int j = 0; j < num[i].size(); j++)
{
cout << "[" << num[i][j] << "]";
}
cout << endl;
}
}
};
string toBinary(int n)
{
string r;
while (n != 0){
r += ( n % 2 == 0 ? "0" : "1" );
n /= 2;
}
return r;
}
class Solution01 {
public:
int getSum(int a, int b) {
if (a == 0) return b;
if (b == 0) return a;
int intTobit = 0;
while(b != 0){
unsigned carry=a&b; // check math carry value.
intTobit = carry;
bitset<32> bs1(toBinary(intTobit));
cout << "carry = " << intTobit << endl;
a=a^b; // sum a and b , without caryy value. if carry value add it , will clear carry bit until 0,
b=carry<<1; //shift carry value until add it.
}
return a;
}
//time complexity O(1)
//space complexity O(1)
};
class Solution02 {
public:
int getSum(int a, int b) {
long mask = 0xFFFFFFFF;
while (b != 0) {
int answer = (a ^ b) & mask;
int carry = ((a & b) & mask) << 1;
a = answer;
b = carry;
}
return a < INT_MAX ? a : ~(a ^ mask);
}
//time complexity O(1)
//space complexity O(1)
};
int main(void)
{
BaseVectorPrint printVector;
Solution01 runfunc;
vector<int> test01 = { 4, 5, 1, 8, 2 };
vector<int> test02 = { 1, 2, 3, 4 };
vector<int> test03 = { -1, 1, 0, -3, 3 };
vector<int> ans ;
int intans;
int a1 = 1;
int b1 = 2;
int a2 = 2;
int b2 = 3;
int a3 = 15;
int b3 = 2;
intans = runfunc.getSum(a3,b3);
//printVector.BasePrint00(ans);
printf("ans = %d\n",intans);
printf("test\n");
system("pause");
}
```