<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> a = "11", b = "1"
<strong>Output:</strong> "100"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> a = "1010", b = "1011"
<strong>Output:</strong> "10101"
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= a.length, b.length &lt;= 10<sup>4</sup></code></li>
	<li><code>a</code> and <code>b</code> consist&nbsp;only of <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code> characters.</li>
	<li>Each string does not contain leading zeros except for the zero itself.</li>
</ul>

class Solution {
public:
    string addBinary(string a, string b) {
        std::stack<char> s;
        string res = "";
        int carry = 0;
        int i=0;
        for(i=0; i < a.length() || i < b.length(); i++) {
            int n0 = 0;
            int n1 = 0;

            if (i < a.length()) {
                n0 = a.at(a.length() - i - 1) - '0';
            }

            if (i < b.length()) {
                n1 = b.at(b.length() - i - 1) - '0';
            }

            int num = n0 + n1 + carry;

            if (num == 0) {
                s.push('0');
               // printf("a[%d]=%d b[%d]=%d, carry=%d, num=%d push 0\n"
               //     , i, n0, i, n1, carry, num);
            } else if (num == 1) {
                s.push('1');
               // printf("a[%d]=%d b[%d]=%d, carry=%d, num=%d push 1\n"
               //     , i, n0, i, n1, carry, num);
                carry = 0;
            } else if (num == 2) {
                s.push('0');
               // printf("a[%d]=%d b[%d]=%d, carry=%d, num=%d push 0\n"
               //     , i, n0, i, n1, carry, num);
                carry = 1;
            } else if (num == 3) {
                s.push('1');
              //  printf("a[%d]=%d b[%d]=%d, carry=%d, num=%d push 1\n"
              //      , i, n0, i, n1, carry, num);
                carry = 1;
            }

            //printf("carry=%d \n", carry);
        }

        if (carry) {
            s.push('1');
           // printf("carry=%d push 1\n" ,carry);
        }

        while(s.size()) {
            res.append(1, s.top());
            s.pop();
        }

       // printf("res=%s\n", res.c_str());
        return res;
    }

    
    /*
    this solution will cause shift overflow than 32bit or 64bit


    uint64_t binaryStringToNumber(const string &s) {
        uint64_t num = 0;
        for (int i=0; i<s.length(); i++) {
            char c = s.at(s.length() - i - 1);
            printf("i=%d, c=%c\n", i, c);

            if (c == '0') {
                continue;
            }

            num += (uint64_t) 1 << i; 
            printf("i=%d, c=%c, add %d, num=%llu\n", i, c
            , (uint64_t) 1 << i, num);
        }

        printf("'%s' num=%d\n", s.c_str(), num);
        return num;
    }

    string numberToBinaryString(uint64_t num) {
        string res = "";
        int has1 = 0;
        for (int i=sizeof(int)*8-1; i >= 0; i--) {
            uint64_t bi = num & (uint64_t 1 << i);
            char c = bi ? '1': '0';

            //printf("i=%d, bi=%d (1 << i)=%d\n", i, bi, 1 << i);

            if (c == '0' && has1 == 0) {
                continue;
            }
            res.append(1, c);

            if (bi) {
                has1++;
            }
        }

        if (res.length() == 0) {
            res = "0";
        }

        printf("num=%d res=%s\n", num, res.c_str());
        return res;
    }

    string addBinary(string a, string b) {
        uint64_t an = binaryStringToNumber(a);
        uint64_t bn = binaryStringToNumber(b);
        return numberToBinaryString(an + bn);
    }
    */
};
9700