Booths Algorithm program in java

import java.util.*;
class BOOTH
{
public static int[] add(int a[],int m1[])
{
int carry=0;
int sum[]=new int [4];
for(int i=3;i>=0;i--)
{
sum[i]=(a[i]+m1[i]+carry)%2;
carry=(a[i]+m1[i]+carry)/2;
}
return sum;
}

public static void shift(int a[],int q[])
{
int b=a[3];
for(int i=2;i>=0;i--)
{
a[i+1]=a[i];
q[i+1]=q[i];
}
q[0]=b;
}

public static int[] comp(int m1[])
{
int z[]={0,0,0,1};
for(int i=0;i<4;i++)
{
if(m1[i]==0)
m1[1]=1;
else
m1[i]=0;
}
int c[]=add(m1,z);
return c;
}

public static void display(int a[],int q[],int Q1,int m[])
{
for(int i=0;i<4;i++)
{
System.out.print(a[i]);
}
System.out.print("\t");
for(int i=0;i<4;i++)
{
System.out.print(q[i]);
}
System.out.print("\t");
System.out.print(Q1);
System.out.print("\t");
for(int i=0;i<4;i++)
{
System.out.print(m[i]);
}
System.out.print("\t");
}

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m[]=new int[4];
int q[]=new int[4];
int a[]={0,0,0,0};
int Q1=0;
int count=4;
int m1[]=new int[4];
System.out.println("ENTER MULTIPLICAND:");
for(int i=0;i<4;i++)
{
m[i]=sc.nextInt();
}
System.out.println("ENTER MULTIPLIER:");
for(int i=0;i<4;i++)
{
q[i]=sc.nextInt();
}
System.out.println("A \t Q \tQ-1\t M \toperation");
display(a,q,Q1,m);
System.out.print("Initial\n");
for(int i=count;i>0;i--)
{
for(int j=0;j<4;j++)
{
m1[j]=m[j];
}
if(q[3]==0&&Q1==1)
{
a=add(a,m1);
display(a,q,Q1,m);
System.out.print("A<~A+M\n");
}
if((q[3]==1&&Q1==1)||(q[3]==3&&Q1==0))
{ }
if(q[3]==1&&Q1==0)
{
int c[]=comp(m1);
a=add(a,c);
display(a,q,Q1,m);
System.out.print("A<~A-M\n");
}
Q1=q[3];
shift(a,q);
display(a,q,Q1,m);
System.out.print("shift\n");
}
}
}

/*

booths Algorithm program in java OUTPUT
BOOTH Algorithm program output:
ENTER MULTIPLICAND:
1
1
1
1
ENTER MULTIPLIER:
1
1
1
1

A Q Q-1 M operation
0000 1111 0 1111 Initial
0001 1111 0 1111 A<~A-M
0000 1111 1 1111 shift
0000 0111 1 1111 shift
0000 0011 1 1111 shift
0000 0001 1 1111 shift */

Facing difficulties in understanding the program ?

Ask Your Queries in Comment Box

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *