Adding two binary numbers I want to improve this code with better logic and using STL.

#include &lt;iostream&gt;
#include &lt;vector&gt;

void addition(const std::vector&lt;int&gt;&amp; A,
              const std::vector&lt;int&gt;&amp; B,
                    std::vector&lt;int&gt;&amp; C)
{
   int size_A = A.size();
   int size_B = B.size();
   int max_size;
   if (size_A &gt;= size_B)
   {
      max_size = size_A;
   }
   else
   {
      max_size = size_B;
   }

   int carry = 0, sum = 0;

   int i = size_A - 1, j = size_B - 1, k = max_size;
   C.resize(k + 1);

   while ( i &gt;= 0 &amp;&amp; j &gt;= 0 &amp;&amp; k &gt;= 0)
   {
      sum = A[i] + B[j] + carry;

      if (sum == 0)
      {
         carry = 0;
         C[k] = 0;
      }
      else if (sum == 1)
      {
         carry = 0;
         C[k] = 1;
      }
      else if (sum == 2)
      {
         carry = 1;
         C[k] = 0;
      }
      else if (sum == 3)
      {
         carry = 1;
         C[k] = 1;
      }
      i--;
      j--;
      k--;
   }
  //if size of first binary number is greter than first
  if (max_size == size_A)
  {
     while (i &gt;= 0 &amp;&amp; k &gt;= 0)
     {
        sum = A[i] + carry;

        if (sum == 0)
        {
           carry = 0;
           C[k] = 0;
        }
        else if (sum == 1)
        {
           carry = 0;
           C[k] = 1;
        }
        else if (sum == 2)
        {
           carry = 1;
           C[k] = 0;
        }
        else if (sum == 3)
        {
           carry = 1;
           C[k] = 1;
        }
        i--;
        k--;
     }
  }
  //If size of second binary number is greater than first
  else if (max_size == size_B)
  {
     while (j &gt;= 0 &amp;&amp; k &gt;= 0)
     {
       sum = B[j] + carry;

       if (sum == 0)
       {
          carry = 0;
          C[k] = 0;
       }
       else if (sum == 1)
       {
          carry = 0;
          C[k] = 1;
       }
       else if (sum == 2)
       {
          carry = 1;
          C[k] = 0;
       }
       else if (sum == 3)
       {
          carry = 1;
          C[k] = 1;
       }
       j--;
       k--;
     }
  }
  C[0] = carry;
}

int main()
{
   int n1, n2, val;
   std::cout &lt;&lt; "Enter the  number of bits for first binary numbers \n";
   std::cin &gt;&gt; n1;
   std::cout &lt;&lt; "Enter the  number of bits for second binary numbers \n";
   std::cin &gt;&gt; n2;

   std::vector&lt;int&gt; A;
   std::vector&lt;int&gt; B;
   std::vector&lt;int&gt; C;

   std::cout &lt;&lt; "Enter bits for first binary number \n";
   for (int i = 0; i &lt; n1; i++)
   {
      std::cin &gt;&gt; val;
      A.push_back(val);
   }

   std::cout &lt;&lt; "Enter bits for second binary number \n";
   for (int i = 0; i &lt; n2; i++)
   {
     std::cin &gt;&gt; val;
     B.push_back(val);
   }

   std::cout &lt;&lt; "First number    : ";
   for (int i = 0; i &lt; A.size(); i++)
   {
      std::cout &lt;&lt; A[i];
   }

   std::cout &lt;&lt; "\nSecond number   : ";
   for (int i = 0; i &lt; B.size(); i++)
   {
      std::cout &lt;&lt; B[i];
   }

   addition(A, B, C);

   std::cout &lt;&lt; "\nResult          : ";
   for (int i = 0; i &lt; C.size(); i++)
   {
     std::cout &lt;&lt; C[i];
   }
   std::cout &lt;&lt; "\n";
}
Write DRY code:

DRY - Don't repeat yourself.

std::cout &lt;&lt; "Enter bits for first binary number \n";
for (int i = 0; i &lt; n1; i++)
{
   std::cin &gt;&gt; val;
   A.push_back(val);
}

std::cout &lt;&lt; "Enter bits for second binary number \n";
for (int i = 0; i &lt; n2; i++)
{
  std::cin &gt;&gt; val;
  B.push_back(val);
}


These two bits of code are way to similar. Make a function that does this work and pass as parameters any differences.

Highest order bit is where?

  std::cin &gt;&gt; val;
  B.push_back(val);


With this code your most significant bit is at location 0. This gives you no room to expand the number. Put the least significant bit at 0 and the more significant bits at higher locations. This will make it easier to do the standard maths operations in the long run.

This looks like a switch:

  if (sum == 0)
  {
     carry = 0;
     C[k] = 0;
  }
  else if (sum == 1)
  {
     carry = 0;
     C[k] = 1;
  }
  else if (sum == 2)
  {
     carry = 1;
     C[k] = 0;
  }
  else if (sum == 3)
  {
     carry = 1;
     C[k] = 1;
  }


I would write as:

  switch (sum) {
      case 0: carry = 0;C[k] = 0;break;
      case 1: carry = 0;C[k] = 1;break;
      case 2: carry = 1;C[k] = 0;break;
      case 3: carry = 1;C[k] = 1;break;
      default: throw std::exception("Something very wrong happened");
  }


Another case of DRY:

  while ( i &gt;= 0 &amp;&amp; j &gt;= 0 &amp;&amp; k &gt;= 0)
  {
      sum = A[i] + B[j] + carry;
      // STUFF
      k--;i--;j--;
  }
  //if size of first binary number is greter than first
  if (max_size == size_A)
  {
     while (i &gt;= 0 &amp;&amp; k &gt;= 0)
     {
        sum = A[i] + carry;
        // STUFF
        k--;i--;
     }
  }
  //If size of second binary number is greater than first
  else if (max_size == size_B)
  {
     while (j &gt;= 0 &amp;&amp; k &gt;= 0)
     {
       sum = B[j] + carry;
       // STUFF
      k--;j--;
     }
  }


Not only can all the "stuff" by factored out into its own function. But those three loops can be combined into a single loop.

  for(;k &gt;= 0;--k)
  {
      int a = (i &gt; 0) ? A[i--] : 0
      int b = (j &gt; 0) ? B[j--] : 0;
      sum = a + b + carry;
      // STUFF
  }