Your Ad Here

Thursday, January 17, 2008

Unsigned Multiplication - C Program

/* C Program For Implementation of Unsigned Multiplication Of Two Numbers */

#include< stdio.h>
#include< conio.h>

#define max 16

void convert1(int *n1,int *n2)
{
int i,z,j,d=0,n=1;
z=(2*max);
for(i=max-1;i>=0;i--)
{
z--;
if(n1[i]==1)
{
n=1;
for(j=1;j< =z;j++)
n = n * 2;
d = d + n;
}
}
n = 1;
for(i=max-1;i>=0;i--)
{
if(n2[i]==1)
{
n = 1;
for(j=1;j< =i;j++)
n = n * 2;
d = d + n;
}
}
printf("\n\n\tIN DECIMAL FORM: %d",d);
}

void convert2(int x,int *arr)
{
int q=0,j=x;
while(j>=0 && q< max)
{
x=j%2;
arr[q++]=x;
j = j/2;
}
}

int addbin(int *n1,int *n2,int *arr3)
{
int i,c=0,g,p,j,arr[max];
for(i=max-1;i>=0;i--)
{
g = n1[i]&n2[i];
p = n1[i]^n2[i];
arr[i] = p^c;
c = g|(p&c);
}
c = 0;
for(i=0;i< max;i++)
{
if(c==0)
{
if(n1[i]==0 && n2[i]==0)
arr3[i]=0;
if(n1[i]==0 && n2[i]==1)
arr3[i]=1;
if(n1[i]==1 && n2[i]==0)
arr3[i]=1;
if(n1[i]==1 && n2[i]==1)
{
arr3[i] = 0;
c = 1;
}
}
else if(c==1)
{
if(n1[i]==0 && n2[i]==0)
{
arr3[i] = 1;
c = 0;
}
if(n1[i]==0 && n2[i]==1)
arr3[i]=0;
if(n1[i]==1 && n2[i]==0)
arr3[i]=0;
if(n1[i]==1 && n2[i]==1)
arr3[i]=1;
}
}
return c;
}

void shift(int *n1,int *n2,int *arr3)
{
int c=0,i,j,k,temp[max];
for(i=0;i< max;i++)
{
if(arr3[0]==1)
{
c = addbin(n1,n2,temp);
for(j=0;j< max;j++)
n1[j] = temp[j];
}
for(j=0;j< max-1;j++)
arr3[j] = arr3[j+1];
arr3[max-1] = n1[0];
for(j=0;j< max-1;j++)
n1[j] = n1[j+1];
n1[max-1] = c;
}
}

void main()
{
int i,j,a[max],b[max],q[max],x,y;
clrscr();
printf("\n _________________________________________________");
printf("\n|\t\tUnsigned Multiplication\n");
printf("|----------------------------------------------------------------------------");
printf("\n|Enter The First No\t: ");
scanf("%d",&x);
printf("|Enter The Second No\t: ");
scanf("%d",&y);
printf("|_________________________________________________");
while(x>0 && y>0)
{
convert2(0,a);
printf("\n\nTHE 16-bit BINARY FORM OF 0\t: ");
for(i=max-1;i>=0;i--)
printf("%d ",a[i]);
convert2(x,b);
printf("\nTHE 16-bit BINARY FORM OF %d\t: ",x);
for(i=max-1;i>=0;i--)
printf("%d ",b[i]);
convert2(y,q);
printf("\nTHE 16-bit BINARY FORM OF %d\t: ",y);
for(i=max-1;i>=0;i--)
printf("%d ",q[i]);
shift(a,b,q);
printf("\n\n\tCalculating %d * %d ",x,y);
printf("In 16-bit BINARY FORM :\n\n\t");
for(i=max-1;i>=0;i--)
printf("%d ",a[i]);
printf("\n\t");
for(j=max-1;j>=0;j--)
printf("%d ",q[j]);
convert1(a,q);
x = -1;
getch();
}
}

/***************** OUTPUT ********************
_________________________________________________
| Unsigned Multiplication
|-------------------------------------------------------------------------
|Enter The First No : 56
|Enter The Second No : 12
|_________________________________________________

THE 16-bit BINARY FORM OF 0 : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
THE 16-bit BINARY FORM OF 56 : 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
THE 16-bit BINARY FORM OF 12 : 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0

Calculating 56 * 12 In 16-bit BINARY FORM :

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0

IN DECIMAL FORM: 672 */

4 comments:

Anonymous said...

Thanks Alot for this code :)

Ur Blog is good !!

To support the HELP i've clicked on Adbrite ;)

cya

sunit said...

plz send me signed multiplication progam if u hav????

sunit said...

plz if u hav send me!!! am waiting

Razye said...

I want a binary multiplication in which get data in binary, operate in binary and then show the result also in bainary. Means do all work in binary.

Your Ad Here