0% found this document useful (0 votes)
4 views1 page

Booths Algorithm Cpp

The document presents a C++ implementation of Booth's Algorithm for binary multiplication. It includes functions for binary addition, arithmetic right shift, and the main algorithm that performs multiplication based on the input multiplicand and multiplier. The program prompts the user for input and outputs the result of the multiplication.

Uploaded by

tinkukaushik785
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Booths Algorithm Cpp

The document presents a C++ implementation of Booth's Algorithm for binary multiplication. It includes functions for binary addition, arithmetic right shift, and the main algorithm that performs multiplication based on the input multiplicand and multiplier. The program prompts the user for input and outputs the result of the multiplication.

Uploaded by

tinkukaushik785
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

// 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;
}

You might also like