Javatpoint Logo
Javatpoint Logo

BOOTHS Algorithm in C

A multiplication algorithm called Booth's algorithm is used to multiply two signed binary values. This algorithm is frequently used in computer maths, which was developed by Andrew Donald Booth in 1951.

The technique increases processing efficiency by reducing the amount of addition operations needed for multiplication. It accomplishes this by performing a number of shifts and adds, which are easily accomplished by straightforward hardware circuits.

In this article, we'll go through Booth's method and show you how to use the C programming language to execute it.

Booth's Algorithm

These observations form the basis of Booth's algorithm:

  • It is possible to use left shift operations to multiply by two.
  • Right-shift operations like division by two are possible.
  • If a binary number consists of a string of consecutive 1s and then 0s, we can replace this string with a single 1 and then the same amount of 0s, and the resulting number will be equal to the original number.

These findings enable the following steps to be used to define Booth's algorithm:

  • Set the item's initial value to 0.
  • Consider expressing the two binary numbers that will be multiplied as two's complements.
  • To make the multiplier's length equal to the multiplicand's length, add a zero bit to the right of the multiplier.
  • Examine two bits at a time, beginning with the multiplier's rightmost bit.
  • Subtract the multiplicand from the product if the first two bits are 0 and 1s.
  • If both bits are 1 and 0, double the product by the multiplicand.
  • Right-shift the multiplier by one bit.
  • Up until all multiplier bits have been checked, repeat steps 4-7.

Application in C

Let's put Booth's algorithm into practice in the C programming language now that we have seen an example of it. We'll create a function that receives two signed binary values as input and outputs the sum of the two.

The function will accept two arguments: a pointer to an array that represents the multiplicand and the multiplier, respectively. Since there are n bits in each number, each array will have an n-bit length. The arrays are going to be considered to be in two's complement form already.

Let's take a look to understand that how the function is implemented:

To understand how it functions, let's walk through this implementation step by step.

Our first step is to allot memory to the product and arrays. In contrast to an array, which stores the value of the register utilized in the method, the product array will carry the multiplication's ultimate result. Initialization value for both arrays is 0.

Next, we run Booth's algorithm by iterating through each bit of the multiplier. We utilize two variables, q0 and q[n-1] to maintain track of the most recent and prior bits of the multiplier.

The current and preceding bits of the multiplier are compared to one of two instances in each iteration: q0 = 0, q[n-1] = 1, or q0 = 1, q[n-1] = 0. After that, depending on the value of the current bit of a, we carry out the necessary operation on the a register by either adding or subtracting the multiplicand.

The current bit of the a register is copied to the least significant bit of q after shifting both a and q to the right by one bit. After that, we carry out this procedure once more for every multiplier bit.

After freeing the memory that the a register utilized and copying its contents to the product array, we return the product array.

Example:

Here is the source code for the C program that uses Booth's algorithm to multiply two signed values. The C program is successfully compiled and executed. Also presented below is the output of the program.

Output:

BOOTH'S MULTIPLICATION ALGORITHM
Enter two numbers to multiply: 
Both must be less than 16
Enter A: 1
Enter B: 11
Expected product = 11

Binary Equivalents are: 
A = 00001
B = 01011
B'+ 1 = 10101

-->
SUB B: 10101:00001
AR-SHIFT: 11010:10000
-->
ADD B: 00101:10000
AR-SHIFT: 00010:11000
-->
AR-SHIFT: 00001:01100
-->
AR-SHIFT: 00000:10110
-->
AR-SHIFT: 00000:01011
Product is = 0000001011

Conclusion:

In conclusion, the Booth algorithm is a helpful technique for binary multiplication of signed integers in the 2's complement representation. Compared to conventional multiplication techniques, the process just requires shifting and either adding or removing the multiplicand based on the multiplier bit value. We have supplied a bitwise operation-based implementation of Booth's method in C along with a practical application.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA