論理的な問題が発生しているプログラムを含めました。プログラムはブースのアルゴリズムに基づいており、その一部を掲載しました。この「作業」スニペットでは、10 進数がユーザーから受け入れられ、配列 (a[0]=1 LSB) を使用して 10 進数形式に変換され、最後に配列 b[] の 2 の補数が計算されます。ここで、プログラムを実行すると:
#include<iostream>
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<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<<"Enter Multiplicand: ";
cin>>bb;
cout<<"Enter Multiplier: ";
cin>>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<<q[3]<<q[2]<<q[1]<<q[0]<<endl; // to display binary no.
//again decimal to binary
while(bb!=0)
{
rem1=bb%2;
bb/=2;
b[j]=rem1;
j++;
}
cout<<b[3]<<b[2]<<b[1]<<b[0]<<endl; //to display binary no.
// 2s complement:
int ii=0;
int jj=4; //4 bit binary number
while(b[ii]==0 && jj!=0)
{
comb[ii]=b[ii];
ii++;
jj--;
}
comb[ii]=b[ii];
cout<<b[3]<<b[2]<<b[1]<<b[0]<<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<<"multiplicand\n";
for(int x=3; x>=0; x--)
{
cout<<b[x]<<" ";
}
cout<<endl;
cout<<"multiplier\n";
for(int j=3; j>(-1); j--)
{
cout<<q[j]<<" ";
}
cout<<endl;
cout<<"compliment of multiplicand\n";
for(int y=3; y>(-1); y--)
{
cout<<comb[y]<<" ";
}
}
int main()
{
booth obj;
cout<<"Booths Algorithm\n";
obj.acceptMm();
obj.display();
return 0;
}
出力
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
この出力では、6 行目が 0101 であると予想されますが、1101 になります。配列 b[] の値が変化するのはなぜですか? 5 行目の配列 b[] の値は正しいのに、なぜ変化するのでしょうか? コードによると、値は正しく変更されるべきではありませんか? 私は立ち往生..助けてください!! どんな提案でも大歓迎です!!