0% found this document useful (0 votes)
8 views3 pages

Booths Algorithm MarzitTalukdar

The document presents an implementation of Booth's Algorithm in C++ for multiplying binary numbers. It includes functions for binary conversion, addition, two's complement, and arithmetic right shift, culminating in the main function that executes the algorithm based on user input. The program outputs both the binary and decimal results 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)
8 views3 pages

Booths Algorithm MarzitTalukdar

The document presents an implementation of Booth's Algorithm in C++ for multiplying binary numbers. It includes functions for binary conversion, addition, two's complement, and arithmetic right shift, culminating in the main function that executes the algorithm based on user input. The program outputs both the binary and decimal results 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/ 3

Booth's Algorithm

Submitted by

Marzit Talukdar
Semester: 4th

Roll No: 230103011

Department of Information Technology

Gauhati University Institute of Science and Technology


// Booth's Algorithm Implementation in C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

string toBinary(int num, int bits) {


string bin = "";
for (int i = bits - 1; i >= 0; i--)
bin += ((num >> i) & 1) ? '1' : '0';
return bin;
}

int binaryToDecimal(string bin) {


int val = 0;
bool isNegative = (bin[0] == '1');
if (isNegative) {
for (char &c : bin)
c = (c == '1') ? '0' : '1';
int carry = 1;
for (int i = bin.size() - 1; i >= 0 && carry; i--) {
if (bin[i] == '1') bin[i] = '0';
else {
bin[i] = '1';
carry = 0;
}
}
}
for (char c : bin)
val = (val << 1) + (c - '0');
return isNegative ? -val : val;
}

string addBinary(string a, string b) {


string res = "";
int carry = 0;
for (int i = a.size() - 1; i >= 0; i--) {
int bitSum = (a[i] - '0') + (b[i] - '0') + carry;
res = char(bitSum % 2 + '0') + res;
carry = bitSum / 2;
}
return res;
}

string twoComplement(string bin) {


string one = string(bin.size() - 1, '0') + '1';
for (char &c : bin)
c = (c == '1') ? '0' : '1';
return addBinary(bin, one);
}

void arithmeticRightShift(string &A, string &Q, char &Q1) {


char lastA = A[0];
Q1 = Q[Q.size() - 1];
Q = A.back() + Q.substr(0, Q.size() - 1);
A = lastA + A.substr(0, A.size() - 1);
}

void boothsAlgorithm(int M, int Qnum, int bits) {


string A(bits, '0'), Q = toBinary(Qnum, bits);
string M_bin = toBinary(M, bits);
string M_neg = twoComplement(M_bin);
char Q1 = '0';

for (int i = 0; i < bits; i++) {


if (Q.back() == '1' && Q1 == '0')
A = addBinary(A, M_neg);
else if (Q.back() == '0' && Q1 == '1')
A = addBinary(A, M_bin);
arithmeticRightShift(A, Q, Q1);
}

string result = A + Q;
cout << "Binary Result: " << result << endl;
cout << "Decimal Result: " << binaryToDecimal(result) << endl;
}

int main() {
int M, Q;
cout << "Enter Multiplicand: ";
cin >> M;
cout << "Enter Multiplier: ";
cin >> Q;
int bits = 5; // adjust based on input range
boothsAlgorithm(M, Q, bits);
return 0;
}

You might also like