Implimentation of unsigned binary multiplication

/*
* Title: Implimentation of unsigned binary multiplication
* Name: Akshay Thakare
* Roll No: 6790
*/

#include
void tobinary(int x,int a[])
{
	printf("\n%d=",x);
	int i=0;
	while(x>0)
	{
		a[i]=x%2;
		x=x/2;
		i++;
	}
	for(i=3;i>=0;i--)
		printf("%d",a[i]);
}

int add(int a[],int b[],int c[])
{
	int co=0,sum,i;
	for(i=0;i0)
	{
		a=0;
		if(q[0]==1)
			a=add(ac,m,ac);
		
		printf("\ncycle %d  ",i);
		a=rightshift(ac,a,4);
		rightshift(q,a,4);
		i--;
	}
}

void print1(int a[])
{
	int i;
	for(i=3;i>=0;i--)
		printf("%d",a[i]);
}

int rightshift(int a[],int cin, int n)
{
	int i,cout=a[0];
	for(i=1;i<n;i++)
	{
		a[i-1]=a[i];
	}
	a[n-1]=cin;
	printf(" ");
	print1(a);
	return cout;
}

int main()
{
	int a,b;
	int ac[4]={0},m[4]={0},q[4]={0};
	printf("Enter multiplicand\n");
	scanf("%d",&a);
	printf("Enter multiplier\n");
	scanf("%d",&b);
	tobinary(a,m);
	tobinary(b,q);
	multi(m,q,ac);
	printf("\nAns=");
	print1(ac);
	print1(q);
}


/*
Output 1
Enter multiplicand
11
Enter multiplier
13

11=1011
13=1101
cycle 4   0101 1110
cycle 3   0010 1111
cycle 2   0110 1111
cycle 1   1000 1111
Ans=10001111

Output 2
Enter multiplicand
8
Enter multiplier
4

8=1000
4=0100
cycle 4   0000 0010
cycle 3   0000 0001
cycle 2   0100 0000
cycle 1   0010 0000
Ans=00100000

Output 3
Enter multiplicand
10
Enter multiplier
2

10=1010
2=0010
cycle 4   0000 0001
cycle 3   0101 0000
cycle 2   0010 1000
cycle 1   0001 0100
Ans=00010100

Output 4
Enter multiplicand
4
Enter multiplier
3

4=0100
3=0011
cycle 4   0010 0001
cycle 3   0011 0000
cycle 2   0001 1000
cycle 1   0000 1100
Ans=00001100
*/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s