# What's wrong with this C++ solution?

• I worked out one implementation of this problem as

``````class Solution {
public:
string addBinary(string a, string b) {
if (a.empty()) {
return b;
}
if (b.empty()) {
return a;
}
int bits = max(a.size(),b.size());
string sum;
char bitA, bitB;
int temp = 0;
for (int i = 0; i<bits; ++i) {
bitA = (a.size()-1-i>=0)?a[a.size()-1-i]:'0';
bitB = (b.size()-1-i>=0)?b[b.size()-1-i]:'0';
int a = (bitA=='1')?1:0;
int b = (bitB=='1')?1:0;
temp = a+b+temp;
char res = ((temp&1)==1)?'1':'0';
sum.insert(sum.begin(), res);
temp = temp>>1;
}
if (temp == 1) {
sum.insert(sum.begin(), '1');
}
return sum;
}
};
``````

The OJ told me wrong answer for this test case:

``````        "1000111111101100010001101101100000011100111001010110000010100111010111000101010001"

"110110101011011101110100000010101111111110011100011011011000110000101010010010010101000110"
``````

The OJ said the results from my code was

``````"111001010100011101100000010100011101011110111001010100101110110011010001101001011010010111"
``````

And the right answer should be

``````"110110110100011101100000010100011101011110111001010100101110110011010001101001011010010111"
``````

Well, I then ran this test case on my machine, the answer returned on my machine was exactly same as the correct answer. How did the OJ get my answer as printed above?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.