/**
*Prepared By: Suresh Ghatuwa, Shreeram Khaitu, Copyright http://www.subirank.com
BOOTH MULTILICATION
*/
#include
#include
#include
#include
int get()
{
int flag=0;
int a;
do
{
printf("ENTER VALUE: ");
scanf("%d",&a);
if(a< 0)
{
a = a * -1;
flag = 1;
}
if(a>=16)
printf("\n\t!INVALID NUMBER.ENTER VALUE (-16 < A < 16)!\n");
}while(a>=16);
if(flag)
a = a *-1;
return(a);
}
void add(int *a,int *b)
{
int x,i,c=0;
for(i=7;i>=0;i--)
{
x=a[i];
a[i]=c^x^b[i];
if(((c==1)&&(x==1))||((x==1)&&(b[i]==1))||((b[i]==1)&&(c==1)))
c = 1;
else
c = 0;
}
}
void binary(int x,int*arr)
{
int i,p=x,c[8]={0,0,0,0,0,0,0,1};
for(i=0;i<8;i++)
arr[i] = 0;
if(x < 0)
x = x *-1;
i = 7;
do
{
arr[i]=x%2;
x = x/2;
i--;
}while(x!=0);
if(p< 0)
{
for(i=0;i< 8;i++)
arr[i]=1-arr[i];
add(arr,c);
}
printf("\n\nTHE BINARY EQUIVALENT OF %d IS : ",p);
for(i=0;i< 8;i++)
printf("%d",arr[i]);
}
void rshift(int x,int *y)
{
int i;
for(i=7;i>0;i--)
y[i] = y[i-1];
y[0] = x;
}
void main()
{ start:
int q=0,i,j,a,b,A[8]={0,0,0,0,0,0,0,0},C[8]={0,0,0,0,0,0,0,1},C1[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
int s=0,z=0,Q[8],M[8],temp,temp1[8],ans[8],y,x=0,c=0,count=8,p;
clrscr();
printf("\n----------------------------------------------------\n");
a = get();
b=get();
printf("%d\n",a);
printf("%d\n",b);
printf("\n---------------------------------------------------\n");
binary(a,M);
binary(b,Q);
printf("\n\n---------------------------------------------------\n");
printf(" Count\t A\t Q\tQ'\t M\t Remark");
printf("\n\n%d\t",count);
for(i=0;i< 8;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 8;i++)
printf("%d",Q[i]);
printf("\t");
printf("%d\t",q);
for(i=0;i< 8;i++)
printf("%d",M[i]);
printf("\tInitial");
printf("\n");
for(j=0;j< 8;j++)
{
if((Q[7]==0)&&(q==1))
{
printf("\n \t");
add(A,M);
for(i=0;i< 8;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 8;i++)
printf("%d",Q[i]);
printf("\t%d\t",q);
for(i=0;i< 8;i++)
printf("%d",M[i]);
printf("\tA:=A+M");
}
if((Q[7]==1)&&(q==0))
{
printf("\n \t");
for(i=0;i< 8;i++)
temp1[i] = 1-M[i];
add(temp1,C);
add(A,temp1);
for(i=0;i< 8;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 8;i++)
printf("%d",Q[i]);
printf("\t%d\t",q);
for(i=0;i< 8;i++)
printf("%d",M[i]);
printf("\tA:=A-M");
}
printf("\n%d\t",--count);
y = A[7];
q = Q[7];
rshift(A[0],A);
rshift(y,Q);
for(i=0;i< 8;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 8;i++)
printf("%d",Q[i]);
printf("\t");
printf("%d\t",q);
for(i=0;i< 8;i++)
printf("%d",M[i]);
printf("\tshift,count--");
printf("\n");
}
printf("\n\n---------------------------------------------------\n");
printf("\nTHE ANSWER IN BINARY IS : ");
for(i=0;i< 8;i++)
ans[i]=A[i];
for(i=0;i< 8;i++)
ans[i+8]=Q[i];
if(((a< 0)&&(b>0))||((a>0)&&(b< 0)))
{
for(i=0;i< 16;i++)
ans[i]=1-ans[i];
for(i=15;i>=0;i--)
{
x = ans[i];
ans[i]=c^x^C1[i];
if(((c==1)&&(x==1))||((x==1)&&(C1[i]==1))||((C1[i]==1)&&(c==1)))
c=1;
else
c=0;
}
}
for(i=0;i< 16;i++)
printf("%d",ans[i]);
for(i=15;i>=0;i--)
{
s = s + (pow(2,z) * ans[i]);
z = z+1;
}
if(((a< 0)&&(b>0))||((a>0)&&(b< 0)))
printf("\nTHE ANSWER IN DECIMAL IS : -%d\n",s);
else
printf("\nTHE ANSWER IN DECIMAL IS : %d\n",s);
printf("\nPress 0 for again and any other for exit:");
scanf("%d",&p);
if(p==0)
{
goto start;
}
getch();
}
Output:
No comments:
Post a Comment