# Simple C++ Solution

• class Solution {
public:

``````string addBinary(string a, string b) {

ReverseString(a);
ReverseString(b);

int i = 0;
int trueCount;
bool carryAOne = false;
string sum;
while (i < a.size() && i < b.size())
{
trueCount = 0;

if (Char2Bin(a[i]))
{
trueCount++;
}
if (Char2Bin(b[i]))
{
trueCount++;
}

Summing(trueCount, carryAOne, sum);

i++;
}

while (i < a.size())
{
trueCount = 0;

if (Char2Bin(a[i]))
{
trueCount++;
}

Summing(trueCount, carryAOne, sum);

i++;
}

while (i < b.size())
{
trueCount = 0;

if (Char2Bin(b[i]))
{
trueCount++;
}

Summing(trueCount, carryAOne, sum);

i++;
}

if (carryAOne)
{
sum.push_back('1');
}

ReverseString(sum);

return sum;
}

void ReverseString(string& s)
{
int strSize = s.size();
int halfStrSize = strSize / 2;
char tempChar;
for (int i = 0; i < halfStrSize; ++i)
{
tempChar = s[i];
s[i] = s[strSize - 1 - i];
s[strSize - 1 - i] = tempChar;
}

return;
}

void Summing(int& trueCount, bool& carryAOne, string& sum)
{
if (carryAOne)
{
trueCount++;
}

if (trueCount > 1)
{
carryAOne = true;
}
else
{
carryAOne = false;
}

if (trueCount % 2 == 0)
{
sum.push_back('0');
}
else
{
sum.push_back('1');
}
}

bool Char2Bin(char& c)
{
if (c == '1')
{
return true;
}

return false;
}
``````

};

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