Binary addition with strings
strings of any length as binary numbers


Why are they strings? Something like bool[] would fit much better and it would also make your code simpler.



string a, string b


Those are not very good names. maybe use something like left and right or first and second?



In production code, you should verify that parameters fit your requirements (in your case, that the string contains only '0's and '1's) and throw an exception otherwise.



if (a.Length &lt; b.Length)
{
    string c = a;
    a = b;
    b = c;
}


A simpler way to write this would be to use recursion:

if (a.Length &lt; b.Length)
{
    return BinAdd(b, a);
}




bool addLeft = false;


A better name for this variable would be carry.



char c = '1';
bool add = addLeft;
addLeft = false;

if (j &gt;= 0)
{
    if (a[i] == '1' &amp;&amp; b[j] == '1')
    {
        c = '0';
        addLeft = true;
    }
    else if (a[i] == '0' &amp;&amp; b[j] == '0')
    {
        c = '0';
    }
    else if (add)
    {
        addLeft = true;
    }
}
else
{
    if (a[i] == '0')
    {
        c = '0';
    }
    else if (add)
    {
        addLeft = true;
    }
}

sb.Append(add ? (c == '1' ? '0' : '1') : c);


All this logic could be simplified by a lot by using a variable to count the ones (including carry). Something like (not tested):

int sum = 0;

if (j &gt;= 0 &amp;&amp; b[j] == '1')
    sum++;

if (a[i] == '1')
    sum++;

if (addLeft)
    sum++;

sb.Append(sum % 2);

addLeft = sum / 2 == 1;




char[] cx = new char[sb.Length];
sb.CopyTo(0, cx, 0, sb.Length);
Array.Reverse(cx);

return new string(cx);


Instead of this, you could start with List&lt;char&gt; instead of StringBuilder, Reverse() that and then create a string out of it (possibly using string.Join(null, digits), assuming you name the list digits).