Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
class Solution
{
public:
    string addBinary(string a, string b)
    {
        int A_SIZE = a.size();
        int B_SIZE = b.size();
        int C_SIZE = (A_SIZE > B_SIZE? A_SIZE : B_SIZE) + 1;
        string Result(C_SIZE, '0');
        unsigned char TmpA = '0';
        unsigned char TmpB = '0';
        unsigned char C = '0';
        unsigned char TmpBit = '0';
        A_SIZE--;
        B_SIZE--;
        while (A_SIZE >= 0 || B_SIZE >= 0)
        {
            TmpA = '0';
            TmpB = '0';
            if (A_SIZE >= 0)
            {
                TmpA = a[A_SIZE--];
            }
            if (B_SIZE >= 0)
            {
                TmpB = b[B_SIZE--];
            }
            unsigned char Count = (TmpA - '0') + (TmpB - '0') + (C - '0');
            TmpBit = Count % 2 + '0';
            C = Count / 2 + '0';
            Result[--C_SIZE] = TmpBit;
        }
        Result[--C_SIZE] = C;
        if (Result[0] == '0')
        {
            return Result.substr(1);
        }
        return Result;
    }
};
原文:http://blog.csdn.net/sheng_ai/article/details/45723861