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

Booth Abr - CPP

The document contains a C++ implementation of Booth's multiplication algorithm, which multiplies two integers using bit manipulation. It initializes an accumulator, multiplier, and multiplicand, then performs a series of arithmetic shifts and conditional additions or subtractions based on the least significant bits. Finally, it outputs the result in both decimal and binary formats.

Uploaded by

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

Booth Abr - CPP

The document contains a C++ implementation of Booth's multiplication algorithm, which multiplies two integers using bit manipulation. It initializes an accumulator, multiplier, and multiplicand, then performs a series of arithmetic shifts and conditional additions or subtractions based on the least significant bits. Finally, it outputs the result in both decimal and binary formats.

Uploaded by

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

#include <iostream>

#include <bitset>

void boothMultiplication(int multiplicand, int multiplier) {


long long A = 0; // Accumulator
long long Q = multiplier; // Multiplier
long long M = multiplicand; // Multiplicand
int Q_1 = 0; // Q-1
int count = 32; // Assuming 32-bit integers
for (int i = 1; i <= count; ++i) {
int Q0 = Q & 1; // Extract the least significant bit of Q

if (Q0 == 1 && Q_1 == 0) {


A = A - M; // Subtract M from A
} else if (Q0 == 0 && Q_1 == 1) {
A = A + M; // Add M to A
}

// Arithmetic shift right


Q_1 = Q0; // Update Q-1
int sign = (A & 0x80000000) != 0 ? 0xFFFFFFFF : 0; // Sign extension for A
Q = ((Q >> 1) & ~0x80000000) | ((A & 1) << 31); // Shift Q right and insert
the sign bit of A
A = (A >> 1) | (sign & 0x80000000); // Shift A right and keep the sign bit

// Combine A and Q to get the result correctly


long long result = (A << 32) | (Q & 0xFFFFFFFF);

// Printing the final result in both decimal and binary formats


std::cout << "Final Result: " << result << " (" << std::bitset<64>(result) <<
")" << std::endl;

int main() {
int multiplicand = -8; // Example multiplicand
int multiplier =3 ; // Example multiplier
boothMultiplication(multiplicand, multiplier);
return 0;
}

You might also like