Booth’s Algorithm

/*
* Title: Implementation of Booth's algorithm
* Name: Akshay Thakare
* Roll No: 6790
*/

#include

void complement(int a[],int b[],int n)
{
	int i=0;
	while(a[i]==0&&i<n)
		b[i]=a[i++];
	b[i]=a[i++];
	while(i<n)
		b[i]=!a[i++];
}

void tobinary(int x,int a[])
{	
	printf("\n%d=",x);
	int i=0,f=0;
	if(x0)
	{
		a[i]=x%2;
		x=x/2;
		i++;
	}
	if(f==1)
		complement(a,a,4);
	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]==0&q1==1)
			a=add(ac,m,ac);
		else if(q[0]==1&q1==0)
			a=add(ac,m1,ac);
		
		printf("\ncycle %d  ",i);
		a=rshift(ac,4);
		q1=rightshift(q,a,4);
		i--;
	}
}

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

int rshift(int a[],int n)//Arithmatic Right Shift
{
	int i,c=a[0];
	for(i=1;i<n;i++)
	{
		a[i-1]=a[i];
	}
	printf(" ");
	print1(a);
	return c;
}
 
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,i,j=0;
	int ac[4]={0},m[4]={0},q[4]={0},ans[8];
	printf("Enter multiplicand\n");
	scanf("%d",&a);
	printf("Enter multiplier\n");
	scanf("%d",&b);
	tobinary(a,m);
	tobinary(b,q);
	multi(m,q,ac);
	for(i=0;i<4;i++)
	{
		ans[j]=q[i];
		j++;
	}
	for(i=0;i=0;i--)
		printf("%d",ans[i]);
	
}

/*
Output 1
Enter multiplicand
-7
Enter multiplier
3 

-7=1001
3=0011
cycle 4   0011 1001
cycle 3   0001 1100
cycle 2   1101 0110
cycle 1   1110 1011
 Answer is -ve
Ans=00010101


Output 2
Enter multiplicand
-7    
Enter multiplier
-3

-7=1001
-3=1101
cycle 4   0011 1110
cycle 3   1110 0111
cycle 2   0010 1011
cycle 1   0001 0101
Ans=00010101

Output 3
Enter multiplicand
7 
Enter multiplier
-3

7=0111
-3=1101
cycle 4   1100 1110
cycle 3   0001 1111
cycle 2   1101 0111
cycle 1   1110 1011
 Answer is -ve
Ans=00010101

Output 4
Enter multiplicand
7  
Enter multiplier
3

7=0111
3=0011
cycle 4   1100 1001
cycle 3   1110 0100
cycle 2   0010 1010
cycle 1   0001 0101
Ans=00010101
*/

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