title: C++ program Booths Algorithm 2s Complement using array
body: <p>I have included the program in which I am having a logical problem. The program is based on booth's algorithm and I have put a snippet of it. In this 'working' snippet decimal number is accepted from the user converted to decimal form with the help of array (a[0]=1 LSB) and lastly the 2s complement is calculated of array b[].
    Now, when I run the program:  </p>
  
  <pre><code>    #include&lt;iostream&gt;
      using namespace std;
      class booth
      {
      public:
      int n;
      int b[3];
      int comb[3], q[3];              //b=multiplicand q=multiplier
      int bb, qq;                     //bb and qq store actual decimal no.s    
      booth()
      {
          for(int i=0; i&lt;4; i++)
          {
              b[i]=0;                 //b array stores multiplicand in binary
              q[i]=0;                 //q array stores multiplier in binary
              comb[i]=0;              //will store calculated 2s complement in      
                                        binary
          }
          n=4;
          bb=0;
          qq=0;
      }
  void acceptMm();
  void display();
  };
  
  void booth :: acceptMm()     //function to accept value from user and                     
                               //converting it into binary in the form of 
                               //array and then calculating its 2s complement
  {
      cout&lt;&lt;"Enter Multiplicand: ";
      cin&gt;&gt;bb;
      cout&lt;&lt;"Enter Multiplier: ";
      cin&gt;&gt;qq;
      //decimal to binary
      int rem1, rem2, i=0, j=0; //rem1 and rem2 are remainders
      while(qq!=0)
      {
          rem2=qq%2;
          qq/=2;
          q[i]=rem2;
          i++;
      }
      cout&lt;&lt;q[3]&lt;&lt;q[2]&lt;&lt;q[1]&lt;&lt;q[0]&lt;&lt;endl;  // to display binary no.
      //again decimal to binary
      while(bb!=0)
      {
          rem1=bb%2;
          bb/=2;
          b[j]=rem1;
          j++;
      }
      cout&lt;&lt;b[3]&lt;&lt;b[2]&lt;&lt;b[1]&lt;&lt;b[0]&lt;&lt;endl;  //to display binary no.
      // 2s complement:
      int ii=0;
      int jj=4;    //4 bit binary number
          while(b[ii]==0 &amp;&amp; jj!=0)
          {
              comb[ii]=b[ii];
              ii++;
              jj--;
          }
          comb[ii]=b[ii];
          cout&lt;&lt;b[3]&lt;&lt;b[2]&lt;&lt;b[1]&lt;&lt;b[0]&lt;&lt;endl;   //displayed value (problem)
          ii++;
          jj--;
          if(jj==0)
          {
              return;
          }
          while(jj!=0)
          {
          if(b[ii]==0)
          {
              comb[ii]=1;
              ii++;
              jj--;   }
          else
          {
              comb[ii]=0;
              ii++;
              jj--;
          }
          }
  }
  
  void booth :: display()
  {
      cout&lt;&lt;"multiplicand\n";
      for(int x=3; x&gt;=0; x--)
          {
              cout&lt;&lt;b[x]&lt;&lt;" ";
          }
      cout&lt;&lt;endl;
      cout&lt;&lt;"multiplier\n";
      for(int j=3; j&gt;(-1); j--)
      {
          cout&lt;&lt;q[j]&lt;&lt;" ";
      }
      cout&lt;&lt;endl;
      cout&lt;&lt;"compliment of multiplicand\n";
      for(int y=3; y&gt;(-1); y--)
      {
          cout&lt;&lt;comb[y]&lt;&lt;" ";
      }
  }
  
  int main()
  {
  booth obj;
  cout&lt;&lt;"Booths Algorithm\n";
      obj.acceptMm();
      obj.display();
  return 0;
  }
  </code></pre>
  
  <blockquote>
    <p>Output</p>
  </blockquote>
  
  <pre><code>Booths Algorithm
  Enter Multiplicand: 5
  Enter Multiplier: 4
  0100
  0101
  1101
  multiplicand
  1 1 0 1 
  multiplier
  0 1 0 0 
  compliment of multiplicand
  0 0 1 1 
  </code></pre>
  
  <p>Here in the output I expect the 6th line as 0101 but get 1101. Why are the values of array b[] changing? The value of array b[] at line 5 is correct so why does it change? According to the code the value shouldnt be changing right?
      I am stuck.. Please help!! Any suggestion will be appreciated!!</p>