Implementation of Booth's Algorithm
Implementation of Booth's Algorithm
Code:
#include <bits/stdc++.h>
using namespace std;
void boothAlgorithm(int br[], int qr[], int mt[], int qrn, int sc)
{
int qn = 0, ac[10] = { 0 };
int temp = 0;
cout << "qn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n";
cout << "\t\t\tinitial\t\t";
display(ac, qr, qrn);
cout << "\t\t" << sc << "\n";
while (sc != 0) {
cout << qr[0] << "\t" << qn;
if ((qn + qr[0]) == 1)
{
if (temp == 0) {
else if (temp == 1)
{
add(ac, br, qrn);
cout << "\t\tA = A + BR\t";
for (int i = qrn - 1; i >= 0; i--)
cout << ac[i];
temp = 0;
}
cout << "\n\t";
rightShift(ac, qr, qn, qrn);
}
sc--;
cout << "\t" << sc << "\n";
}
}
brn = 4;
int br[] = { 0, 1, 1, 0 };
qrn = 4;
sc = qrn;
int qr[] = { 0, 0, 1, 0 };
reverse(qr, qr + qrn);
boothAlgorithm(br, qr, mt, qrn, sc);
cout << endl
<< "Result = ";
for (int i = qrn - 1; i >= 0; i--)
cout << qr[i];
}
Output:
Conclusion: From this experiment we implemented booth’s algorithm using C+
+ programming language.