Experiment No- 08
Aim: - To implement Booth’s Algorithm
Theory: -
Booth algorithm gives a procedure for multiplying binary
integers in signed 2’s complement representation in efficient
way, i.e., less number of additions/subtractions required. It
operates on the fact that strings of 0’s in the multiplier
require no addition but just shifting and a string of 1’s in the
multiplier from bit weight 2^k to weight 2^m can be treated
as 2^(k+1 ) to 2^m.
As in all multiplication schemes, booth algorithm requires
examination of the multiplier bits and shifting of the partial
product. Prior to the shifting, the multiplicand may be added
to the partial product, subtracted from the partial product,
or left unchanged according to following rules:
1. The multiplicand is subtracted from the partial product
upon encountering the first least significant 1 in a string
of 1’s in the multiplier
2. The multiplicand is added to the partial product upon
encountering the first 0 (provided that there was a
previous ‘1’) in a string of 0’s in the multiplier
3. The partial product does not change when the multiplier
bit is identical to the previous multiplier bit.
Flowchart :-
Program :
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int a=0,b=0,c=0,a1=0,b1=0,com[5]= {1,0,0,0,0};
int anum[5]= {0},anumcp[5]= {0},bnum[5]= {0};
int acomp[5]= {0},bcomp[5]= {0},pro[5]= {0},res[5]= {0};
void binary()
{
int r,r2,i,temp; a1=fabs(a); b1=fabs(b);
for(i=0; i<5; i++)
{
r=a1%2;
a1=a1/2;
r2=b1%2;
b1=b1/2;
anum[i]=r;
anumcp[i]=r;
bnum[i]=r2;
if(r2==0)
{
bcomp[i]=1;
}
if(r==0)
{
acomp[i]=1;
}
}
c=0;
for(i=0; i<5; i++)
{
res[i]=com[i]+bcomp[i]+c;
if(res[i]>=2)
{
c=1;
}
else
c=0;
res[i]=res[i]%2;
}
for(i=4; i>=0; i--)
{
bcomp[i]=res[i];
}
if(a<0)
{
c=0;
for(i=4; i>=0; i--)
{
res[i]=com[i]+acomp[i]+c;
if(res[i]>=2)
{
c=1;
}
else
c=0;
res[i]=res[i]%2;
}
for(i=4; i>=0; i--)
{
anum[i]=res[i];
anumcp[i]=res[i];
}
}
if(b<0)
{
for(i=0; i<5; i++)
{
temp=bnum[i];
bnum[i]=bcomp[i];
bcomp[i]=temp;
}
}
}
void add(int num[])
{
int i;
c=0;
for(i=0; i<5; i++)
{
res[i]=pro[i]+num[i]+c;
if(res[i]>=2)
{
c=1;
}
else
{
c=0;
}
res[i]=res[i]%2;
}
for(i=4; i>=0; i--)
{
pro[i]=res[i];
printf("%d",pro[i]);
}
printf(":");
for(i=4; i>=0; i--)
{
printf("%d",anumcp[i]);
}
}
void arshift()
{
int temp=pro[4],temp2=pro[0],i;
for(i=1; i<5; i++)
{
pro[i-1]=pro[i];
}
pro[4]=temp;
for(i=1; i<5; i++)
{
anumcp[i-1]=anumcp[i];
}
anumcp[4]=temp2;
printf("\nAR-SHIFT: ");
for(i=4; i>=0; i--)
{
printf("%d",pro[i]);
}
printf(":");
for(i=4; i>=0; i--)
{
printf("%d",anumcp[i]);
}
}
void main()
{
int i,q=0;
printf("\n\t\tBOOTH`S MULTIPLICATION ALGORITHM\n\n");
printf("Name:- Harshdeep Sirsat\n");
printf("Roll no.:- 96\n");
printf("\n Enter two numbers to multiply: ");
printf("\nBoth must be less than 16");
do
{
printf("\nEnter A: "); scanf("%d",&a);
printf("\nEnter B: "); scanf("%d",&b);
}
while(a>=16||b>=16);
printf("\nExpected product=%d",a*b);
binary();
printf("\n\nBinary Equivalents are:");
printf("\nA=");
for(i=4; i>=0; i--)
{
printf("%d",anum[i]);
}
printf("\nB= ");
for(i=4; i>=0; i--)
{
printf("%d",bnum[i]);
}
printf("\nB`+1=");
for(i=4; i>=0; i--)
{
printf("%d",bcomp[i]);
}
printf("\n\n");
getchar();
for(i=0; i<5; i++)
{
if(anum[i]==q)
{
printf("\n--> ");
arshift();
q=anum[i];
}
else if(anum[i]==1 && q==0)
{
printf("\n-->");
printf("\nSUB B: ");
add(bcomp);
arshift();
q=anum[i];
}
else
{
printf("\n-->");
printf("\nADD B: ");
add(bnum);
arshift();
q=anum[i];
}
}
printf("\nProduct is = ");
for(i=4; i>=0; i--)
{
printf("%d",pro[i]);
}
for(i=4; i>=0; i--)
{
printf("%d",anumcp[i]);
}
getchar();
}
Output :
Conclusion: Booth's algorithm is implemented using C language and
output is verified.