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