title: how can I implement adding "+1" step when calculating two's complement for a negative number
body: <p>The program works fine for unsigned integers for any given Length but for negative integers, if the integer is -99 and length is 8 then the output is 10011100 however correct answer is 10011101. so, basically, the "+1" step is missing. How can I code it without adding a new library or using arrays? </p>
  
  <p>$</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "math.h"
  #include &lt;string&gt;
  using namespace std;
  // DONT MAKE ANY ADDITION
  // DONT USE ANY ARRAY DATA TYPE
  
  string decimalToTwoComplimentString(int a, int length)
  {
      int bitSize=0;
      string binary, r_bin;
  
      for(int j=abs(a); j&gt;=0; j/=2)
      {
          if(a&gt;=0) // NO PROBLEM HERE
          {
              if(bitSize!=length)
              {
                  if(j%2==0)
                      binary.append("0");
                  else if(j%2==1)
                      binary.append("1");
                  bitSize++;
              }
              else if(bitSize==length)
              {
                  break;
              }
          }
  
          else if(a&lt;0) // ADDING +1 AT THE END PART IS MISSING!
          {
              if(bitSize!=length)
              {
                  if(j%2==0)
                      binary.append("1");
                  else if(j%2==1)
                      binary.append("0");
                  bitSize++;
              }
              else if(bitSize==length)
              {
                  break;
              }
          }
      }
  
      for(int i=binary.length()-1; i&gt;=0; i--) // PRINTING STRING BACKWARDS
      {
          r_bin+=binary.at(i);
      }
      return r_bin;
  }
  
  int main()
  {
      int L;
      cout&lt;&lt; "Enter bit pattern size";
      cin&gt;&gt;L;
  
      int a, b;
      cout&lt;&lt;"Enter an integer a ";
      cin&gt;&gt;a;
      cout&lt;&lt;"Enter an integer b ";
      cin&gt;&gt;b;
  
      int c1 = a + b;
      cout&lt;&lt;"In decimal "&lt;&lt; a &lt;&lt; " + " &lt;&lt; b &lt;&lt; " is " &lt;&lt; c1 &lt;&lt; endl;
  
      string A = decimalToTwoComplimentString(a, L);
      string B = decimalToTwoComplimentString(b, L);
  
      cout&lt;&lt; "The Two's complement of " &lt;&lt; a &lt;&lt; " is \t" &lt;&lt; A &lt;&lt; endl;
      cout&lt;&lt; "The Two's complement of " &lt;&lt; b &lt;&lt; " is \t" &lt;&lt; B &lt;&lt; endl;
  
  
      system("Pause");
      return 0;
  }
  </code></pre>