// Implementation of Booth's Algorithm in C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int addBinary(int a, int b) {
int carry;
while (b != 0) {
carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
int arithmeticRightShift(int a, int &q, int &q1) {
int temp = q;
q = (q >> 1) | ((a & 1) << 31);
a = (a >> 1);
q1 = temp & 1;
return a;
}
void boothsAlgorithm(int m, int q, int n) {
int a = 0;
int q1 = 0;
int count = n;
while (count > 0) {
if ((q & 1) == 1 && q1 == 0) {
a = a - m;
} else if ((q & 1) == 0 && q1 == 1) {
a = a + m;
}
a = arithmeticRightShift(a, q, q1);
count--;
}
cout << "Result: " << q << endl;
}
int main() {
int multiplicand, multiplier;
cout << "Enter Multiplicand: ";
cin >> multiplicand;
cout << "Enter Multiplier: ";
cin >> multiplier;
boothsAlgorithm(multiplicand, multiplier, 5); // assuming 5-bit numbers
return 0;
}