Booth'S Multiplication
Booth'S Multiplication
#include<conio.h>
#include<stdio.h>
/*Global variables*/
int Q[5]={0,0,0,0,0};
int A[5]={0,0,0,0,0};
int M[5]={0,0,0,0,0};
int Mcomp[5]={0,0,0,0,0};
int Qcomp[5]={0,0,0,0,0};
int result[5]={0,0,0,0,0};
int addarr[5]={0,0,0,0,1};
int res2[5]={0,0,0,0,0};
int Mtwos[5]={0,0,0,0,0};
int Qtwos[5]={0,0,0,0,0};
int q1;
/*static functions*/
void shift();
void convertbin(int);
void onescomp(int[]);
void add(int[],int[]);
void print();
int main()
{
int m,n,q,i,mdup,qdup;
printf("\nEnter the multiplicand(M): \t");
scanf("%d",&m);
mdup=m;
/*Code to convert multiplicand in binary form*/
if(m<0)
{
m=m*(-1);
convertbin(m);
for(i=0;i<=4;i++)
M[i]=result[i];
onescomp(M);
add(addarr,Mcomp);
printf("\nThe binary equivalent of M =\t");
for(i=0;i<=4;i++)
{
Mtwos[i]=M[i];
M[i]=res2[i];
printf("%d",M[i]);
}
printf("\n");
printf("The two's complement of M=\t");
for(i=0;i<=4;i++)
printf("%d",Mtwos[i]);
printf("\n");
}
else
{
convertbin(m);
printf("\nThe binary equivalent of M =\t");
for(i=0;i<=4;i++)
{
M[i]=result[i];
printf("%d",result[i]);
}
printf("\n");
onescomp(M);
add(addarr,Mcomp);
for(i=0;i<=4;i++)
Mtwos[i]=res2[i];
printf("\n");
printf("The two's complement of M =\t");
for(i=0;i<=4;i++)
printf("%d",Mtwos[i]);
printf("\n");
}
/*Code to convert multiplier in binary form*/
printf("\nEnter the multiplier(Q) : \t");
scanf("%d",&q);
qdup=q;
if(q<0)
{
q=q*(-1);
convertbin(q);
for(i=0;i<=4;i++)
Q[i]=result[i];
onescomp(Q);
for(i=0;i<=4;i++)
Qcomp[i]=Mcomp[i];
add(addarr,Qcomp);
printf("\nThe binary equivalent of Q =\t");
for(i=0;i<=4;i++)
{
Qtwos[i]=Q[i];
Q[i]=res2[i];
printf("%d",Q[i]);
}
printf("\n\n");
}
else
{
convertbin(q);
printf("\nThe binary equivalent of Q =\t");
for(i=0;i<=4;i++)
{
Q[i]=result[i];
printf("%d",result[i]);
}
printf("\n\n");
}
q1=0;
printf("\n----------------------------------------------------------------------\n\n");
printf("
STEP\t\t");
printf("A\t\t");
printf("Q\t\t");
printf("Q-1\t\t");
printf("M\t\t");
printf("\n----------------------------------------------------------------------\n");
printf("\n
print();
INITIAL\t");
/*ALGORITHM*/
for(n=1;n<=5;n++)
{
if(Q[4]==q1)
{
printf("\n %d SHIFT\t",n);
shift();
print();
}
A[i]=res2[i];
}
print();
printf("
SHIFT\t",n);
shift();
print();
}
}
printf("\n\n
THE PRODUCT \n\n
IN BINARY
for(i=0;i<=4;i++)
{
printf("%d",A[i]);
}
printf(" ");
for(i=0;i<=4;i++)
{
printf("%d",Q[i]);
}
printf("\n\n
IN DECIMAL IS : \t\t");
printf("%d",(mdup*qdup));
getch();
} //end of main
void convertbin(int a)
{
int i=4;
for(i=0;i<=4;i++)
result[i]=0;
i=4;
while(a!=0)
{
result[i]=a%2;
a=a/2;
i--;
}
}
IS :\t\t");
if((one[i]+comp[i]+carry)==0)
{
res2[i]=0;
carry=0;
}
else if((one[i]+comp[i]+carry)==1)
{
res2[i]=1;
carry=0;
}
else if((one[i]+comp[i]+carry)==2)
{
res2[i]=0;
carry=1;
}
else if((one[i]+comp[i]+carry)>2)
{
res2[i]=1;
carry=1;
}
}
}
void shift()
{
q1=Q[4];
for(int i=3;i>=0;i--)
Q[i+1]=Q[i];
Q[0]=A[4];
for(int j=3;j>=0;j--)
A[j+1]=A[j];
A[0]=A[0];
}
void print()
{
int i;
for(i=0;i<=4;i++)
printf("%d",A[i]);
printf("\t\t");
for(i=0;i<=4;i++)
printf("%d",Q[i]);
printf("\t\t");
printf("%d\t\t",q1);
for(i=0;i<=4;i++)
printf("%d",M[i]);
printf("\n");
}
/* OUTPUT:
00101
The two's
11011
complement of M =
-3
11101
----------------------------------------------------------------------STEP
Q-1
----------------------------------------------------------------------INITIAL
00000
11101
00101
1 SUBTRACT
SHIFT
11011
11101
11101
11110
0
1
00101
00101
2 ADD
SHIFT
00010
00001
11110
01111
1
0
00101
00101
3 SUBTRACT
SHIFT
11100
11110
01111
00111
0
1
00101
00101
4 SHIFT
11111
00011
00101
5 SHIFT
11111
10001
00101
THE PRODUCT
IN BINARY
IS :
IN DECIMAL IS :
*/
11111 10001
-15