title: Adding 2 binary numbers as string, C++
body: <p>I have a problem with adding 2 binary numbers. I want to do this as a string, so if they're different lengths I concatenate '0' to the beginning of shorter string. First of all, I don't know why, but I need to substrate then add '0' (without it, it wasn't working at all).</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  
  string add( string no1, string no2 );
  
  
  int equalizer(string no1, string no2)
  {
  int len1 = no1.length();
  int len2 = no2.length();
  if (len1 &lt; len2)
  {
      for (int i = 0 ; i &lt; len2 - len1 ; i++)
      {
  
          no1 = '0' + no1;
      }
      return len2;
   }
   else if (len1 &gt;= len2)
   {
      for (int i = 0 ; i &lt; len1 - len2 ; i++)
      {
  
          no2 = '0' + no2;
      }
      return len1; // If len1 &gt;= len2
   }
  
   }
  
  
   string add( string no1, string no2 )
   {
    string result="";
  
  
    int length = equalizer(no1, no2);
  
   int carry = 0;
  
  
   for (int i = length-1 ; i &gt;= 0 ; i--)
   {
      int bit1 = no1.at(i) - '0';
      int bit2 = no2.at(i) - '0';
  
      // boolean expression for sum of 3 bits
      int sum = (bit1 ^ bit2 ^ carry)+'0';
  
      result = (char)sum + result;
  
      // boolean expression for 3-bit addition
      carry = (bit1 &amp; bit2) | (bit2 &amp; carry) | (bit1 &amp; carry);
    } 
  
    // if overflow, then add a leading 1
    if (carry)
   {
  
  
      result = '1' + result;
    }
  
    return result;
    }
   bool check(string no1)
   {
   for(int i =0; i&lt;no1.length(); i++)
   {
      if(no1.at(i)!=0 || no1.at(i)!=1)
      {
          cout &lt;&lt; "not biniary! should contain only '0' and '1' "&lt;&lt; endl;
          return false;
      }
      else
      {
          return true;
      }
     }
    }
   int main()
   {
   string no1;
   string no2;
   cout &lt;&lt; "Welcome to program that add 2 biniary numbers!" &lt;&lt; endl;
   cout &lt;&lt;"Give first number " &lt;&lt;endl;
   cin &gt;&gt; no1;
   if(check(no1)==true)
   {
  
  
  
  
  
      cout &lt;&lt;"Give 2nd number" &lt;&lt; endl;
      cin &gt;&gt; no2;
      check(no2);
      cout &lt;&lt; "Numbers are proper!" &lt;&lt; endl;
      add(no1,no2);
   }
   else
   {
      cout &lt;&lt;"End of program."&lt;&lt;endl;
   }
   return 0;
   }
  </code></pre>