Implement Booth's Multiplication Algorithm in C++



Booth's algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2's complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed.

In this article, we have an array of multiplicand bits and an array of multiplier bits. Our task is to use Booth's algorithm to find the multiplication of these two binary numbers.

Example of Booth's Algorithm

In this example, we have used Booth's algorithm to calculate the multiplication of two signed binary numbers mathematically.

Input:
Multiplier(M): 01011 = 11
Multiplicand(Q): 01110 = 14

Output:
Product: 0100 11010 = 154

The explanation of the above example is as follows:

Complement of (M'): 10101
Q = 01110 = 2^4 - 2^1
M * Q = 2^4(M) - 2^1(M)
M * Q = 2^4(M) + 2^1(M')

2^4(M) = 0101 10000  (Left shift of M by 4 bits)
2^1(M') = 1111 01010  (Left shift of M' by 1 bit)

M * Q = 2^4(M) + 2^1(M'):
= 0101 10000 + 1111 01010
= 0100 11010 = 154

Booth's Multiplication Operations

We will follow these operations based on the value of qn and q[n+1]. The qn represents the least significant bit (LSB) of the multiplier and the q[n+1] here represents the single bit register used to store the previous LSB of qn after the right shift operation.

qn q[n+1] Operation
0 0 Perform arithmetic right shift(ARS)
0 1 accumulator + multiplicand then ARS
1 0 accumulator - multiplicand then ARS
1 1 Perform arithmetic right shift(ARS)

Steps to Implement Booth's Multiplication Algorithm

The steps for implementing Booth's multiplication algorithm are given below:

  • First, we have declared the multiplicand array br[], multiplier array qr[], an accumulator array ac[], and an array mt[] that store the 2's complement of the multiplicand.
  • Then, we check the least significant bit (LSB) of the multiplier array qr[]. The qn stores the LSB of the multiplier array after the right shift and is initially initialized to 0.
  • Based on the LSB value of qr and the qn value, we perform the operations mentioned in the above table.
  • If the LSB value of qr and qn is either both 0 or both 1, then we right shift the accumulator and multiplier bit by 1 digit using the ars() function and it is stored in the accumulator array ac[].
  • If the value of LSB is 0 and qn is 1, then we use the add() function to perform the addition of accumulator and multiplicand bits and right shift after the addition.
  • If the value of LSB is 1 and qn is 0, then we perform the subtraction of accumulator and multiplicand bits. We have used 2's complement to convert the multiplicand bit into a negative value using the complement() function.
  • We repeat the above three steps until the step count reaches 0 or sc=0.
  • The product of both binary numbers is then displayed using the display() function.

C++ Implementation of Booth's Multiplication Algorithm

Here is the C++ implementation of the above steps to calculate the product of two binary numbers using Booth's multiplication algorithm.

#include <iostream>
using namespace std;

void add(int a[], int x[], int q);
void complement(int a[], int n)
{
    int i;
    int x[8] = {0};
    x[0] = 1;
    for (i = 0; i < n; i++)
    {
        a[i] = (a[i] + 1) % 2;
    }
    add(a, x, n);
}

void add(int ac[], int x[], int q)
{
    int i, c = 0;
    for (i = 0; i < q; i++)
    {
        ac[i] = ac[i] + x[i] + c;
        if (ac[i] > 1)
        {
            ac[i] = ac[i] % 2;
            c = 1;
        }
        else
            c = 0;
    }
}

void ars(int ac[], int qr[], int &qn, int q)
{
    int temp, i;
    temp = ac[0];
    qn = qr[0];
    cout << "\t\tars\t\t";
    for (i = 0; i < q - 1; i++)
    {
        ac[i] = ac[i + 1];
        qr[i] = qr[i + 1];
    }
    qr[q - 1] = temp;
}

void display(int ac[], int qr[], int qrn)
{
    int i;
    for (i = qrn - 1; i >= 0; i--)
        cout << ac[i];
    cout << " ";
    for (i = qrn - 1; i >= 0; i--)
        cout << qr[i];
}

int main(int argc, char **argv)
{
    int mt[10], br[10], qr[10], sc, ac[10] = {0};
    int brn, qrn, i, qn, temp;

    brn = 5;                          // number of bits for multiplicand
    int br_input[] = {0, 1, 0, 1, 1}; // multiplicand in 2's complement 
    cout << "Multiplicand Value: ";
    for (int i = 0; i<brn; i++){
        cout << br_input[i] << " ";
    }
    for (i = brn - 1; i >= 0; i--)
        br[i] = br_input[brn - 1 - i];

    for (i = brn - 1; i >= 0; i--)
        mt[i] = br[i];
    complement(mt, brn);

    qrn = 5; // number of bits for multiplier
    sc = qrn;
    int qr_input[] = {0, 1, 1, 1, 0}; // multiplier in 2's complement 
    cout << "\nMultiplier Value: ";
    for (int i = 0; i<brn; i++){
        cout << qr_input[i] << " ";
    }
    for (i = qrn - 1; i >= 0; i--)
        qr[i] = qr_input[qrn - 1 - i];

    qn = 0;
    temp = 0;

    cout << "\nqn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n";
    cout << "\t\t\tinitial\t\t";
    display(ac, qr, qrn);
    cout << "\t\t" << sc << "\n";

    while (sc != 0)
    {
        cout << qr[0] << "\t" << qn;
        if ((qn + qr[0]) == 1)
        {
            if (temp == 0)
            {
                add(ac, mt, qrn);
                cout << "\t\tsubtracting BR\t";
                for (i = qrn - 1; i >= 0; i--)
                    cout << ac[i];
                temp = 1;
            }
            else if (temp == 1)
            {
                add(ac, br, qrn);
                cout << "\t\tadding BR\t";
                for (i = qrn - 1; i >= 0; i--)
                    cout << ac[i];
                temp = 0;
            }
            cout << "\n\t";
            ars(ac, qr, qn, qrn);
        }
        else if (qn - qr[0] == 0)
            ars(ac, qr, qn, qrn);

        display(ac, qr, qrn);
        cout << "\t";
        sc--;
        cout << "\t" << sc << "\n";
    }

    cout << "Product=";
    display(ac, qr, qrn);
}

The output of the above code is as follows:

Multiplicand Value: 0 1 0 1 1 
Multiplier Value: 0 1 1 1 0 
qn	q[n+1]		BR		AC	QR		sc
			initial		00000 01110		5
0	0		ars		00000 00111		4
1	0		subtracting BR	10101
			ars		11010 10011		3
1	1		ars		11101 01001		2
1	1		ars		11110 10100		1
0	1		adding BR	01001
			ars		00100 11010		0
Product=00100 11010
Updated on: 2025-06-03T17:46:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements