/* File Name: bnew.java
Program to Implement Bhoots Algrithms.
Learner:-ABDUL MUGEESH
RollNo:-12CO104 */
import java.io.*;
import java.util.*;
class booth
{
static void add(int a[],int b[],int ad)
{
int i,j,c=0;
for(j=0;j<=ad-1;j++)
{
a[j]=a[j]+b[j]+c;
c=0;
if (a[j]==2)
{
a[j]=0;
c=1;
}
else if (a[j]==3)
{
a[j]=1;
c=1;
}
}
}
static void sub(int a[],int b[],int n)
{
int cb,borrow=0,bit,result;
int c=n;
int res[]=new int[c];
System.out.println();
for(bit=0;bit<=n-1;bit++)
{
result=a[bit]-b[bit]-borrow;
if (result<0)
{
result=result*(-1);
borrow=1;
}
else
{
borrow=0;
}
a[bit]=result%2;
}
}
static void display(int a[],int b[],int q[],int q1,int n)
{
int i;
for(i=n-1;i>=0;i--)
{
System.out.print(a[i]);
}
System.out.print("\t");
for(i=n-1;i>=0;i--)
{
System.out.print(q[i]);
}
System.out.print("\t");
System.out.print(q1);
System.out.print("\t");
for(i=n-1;i>=0;i--)
{
System.out.print(b[i]);
}
System.out.println(" ");
}
public static void main(String ar[])
{
String nt,mt,qt;
int n,i,j,m,qm,mod,count,c1,ad;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter no of bits :: ");
nt=br.readLine();
n=Integer.parseInt(nt);
c1=n;
ad=n;
int q1=0;
int a[]=new int[n];
int b[]=new int[n];
int q[]=new int[n];
int z[]=new int[n];
z[0]=1;
for(i=1;i<=n-1;i++)
{
z[i]=0;
}
System.out.print("\nEnter the Multiplicand :: ");
mt=br.readLine();
m=Integer.parseInt(mt);
int sm=m;
System.out.print("\nEnter the Multiplier ::");
qt=br.readLine();
qm=Integer.parseInt(qt);
int sq=qm;
System.out.println("A"+"\t"+"Q"+"\t"+"Q1"+"\t"+"M");
for(i=0;i<=n-1;i++)
{
mod=m%2;
m=m/2;
b[i]=mod;
}
if (sm<0)
{
for(i=0;i<=n-1;i++)
{
b[i]=b[i]*(-1);
}
for(i=0;i<=n-1;i++)
{
if (b[i]==0)
b[i]=1;
else if(b[i]==1)
b[i]=0;
}
for(i=0;i<=n-1;i++)
{
b[i]=b[i]+z[i];
if (b[i]==2)
{
b[i]=0;
b[i+1]=b[i+1]+1;
}
else if (b[i]==3)
{
b[i]=1;
b[i+1]=b[i+1]+1;
}
}
}
for(i=0;i<=n-1;i++)
{
mod=qm%2;
qm=qm/2;
q[i]=mod;
}
if (sq<0)
{
for(i=0;i<=n-1;i++)
{
q[i]=q[i]*(-1);
}
for(i=0;i<=n-1;i++)
{
if (q[i]==0)
q[i]=1;
else if(q[i]==1)
q[i]=0;
}
for(i=0;i<=n-1;i++)
{
q[i]=q[i]+z[i];
if (q[i]==2)
{
q[i]=0;
q[i+1]=q[i+1]+1;
}
else if (q[i]==3)
{
q[i]=1;
q[i+1]=q[i+1]+1;
}
}
}
display(a,b,q,q1,n);
while (c1!=0)
{
if (q[0]==0&&q1==1)
{
add(a,b,n);
display(a,b,q,q1,n);
}
else
if (q[0]==1&&q1==0)
{
sub(a,b,n);
display(a,b,q,q1,n);
}
q1=q[0];
for(i=0;i<n-1;i++)
{
q[i]=q[i+1];
}
q[n-1]=a[0];
for(i=0;i<n-1;i++)
{
a[i]=a[i+1];
}
c1=c1-1;
display(a,b,q,q1,n);
System.out.println();
}
System.out.print("\nMultiplication is = ");
for(i=n-1;i>=0;i--)
{
System.out.print(a[i]);
}
for(i=n-1;i>=0;i--)
{
System.out.print(q[i]);
}
System.out.println();
System.out.println();
}
catch(Exception e)
{}
}
}
/*
Enter no of bits :: 4
Enter the Multiplicand :: 7
Enter the Multiplier :: 3
A Q Q1 M
0000 0011 0 0111
1001 0011 0 0111
1100 1001 1 0111
1110 0100 1 0111
0101 0100 1 0111
0010 1010 0 0111
0001 0101 0 0111
Multiplication is = 00010101
Process Exit...
*/
Program to Implement Bhoots Algrithms.
Learner:-ABDUL MUGEESH
RollNo:-12CO104 */
import java.io.*;
import java.util.*;
class booth
{
static void add(int a[],int b[],int ad)
{
int i,j,c=0;
for(j=0;j<=ad-1;j++)
{
a[j]=a[j]+b[j]+c;
c=0;
if (a[j]==2)
{
a[j]=0;
c=1;
}
else if (a[j]==3)
{
a[j]=1;
c=1;
}
}
}
static void sub(int a[],int b[],int n)
{
int cb,borrow=0,bit,result;
int c=n;
int res[]=new int[c];
System.out.println();
for(bit=0;bit<=n-1;bit++)
{
result=a[bit]-b[bit]-borrow;
if (result<0)
{
result=result*(-1);
borrow=1;
}
else
{
borrow=0;
}
a[bit]=result%2;
}
}
static void display(int a[],int b[],int q[],int q1,int n)
{
int i;
for(i=n-1;i>=0;i--)
{
System.out.print(a[i]);
}
System.out.print("\t");
for(i=n-1;i>=0;i--)
{
System.out.print(q[i]);
}
System.out.print("\t");
System.out.print(q1);
System.out.print("\t");
for(i=n-1;i>=0;i--)
{
System.out.print(b[i]);
}
System.out.println(" ");
}
public static void main(String ar[])
{
String nt,mt,qt;
int n,i,j,m,qm,mod,count,c1,ad;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter no of bits :: ");
nt=br.readLine();
n=Integer.parseInt(nt);
c1=n;
ad=n;
int q1=0;
int a[]=new int[n];
int b[]=new int[n];
int q[]=new int[n];
int z[]=new int[n];
z[0]=1;
for(i=1;i<=n-1;i++)
{
z[i]=0;
}
System.out.print("\nEnter the Multiplicand :: ");
mt=br.readLine();
m=Integer.parseInt(mt);
int sm=m;
System.out.print("\nEnter the Multiplier ::");
qt=br.readLine();
qm=Integer.parseInt(qt);
int sq=qm;
System.out.println("A"+"\t"+"Q"+"\t"+"Q1"+"\t"+"M");
for(i=0;i<=n-1;i++)
{
mod=m%2;
m=m/2;
b[i]=mod;
}
if (sm<0)
{
for(i=0;i<=n-1;i++)
{
b[i]=b[i]*(-1);
}
for(i=0;i<=n-1;i++)
{
if (b[i]==0)
b[i]=1;
else if(b[i]==1)
b[i]=0;
}
for(i=0;i<=n-1;i++)
{
b[i]=b[i]+z[i];
if (b[i]==2)
{
b[i]=0;
b[i+1]=b[i+1]+1;
}
else if (b[i]==3)
{
b[i]=1;
b[i+1]=b[i+1]+1;
}
}
}
for(i=0;i<=n-1;i++)
{
mod=qm%2;
qm=qm/2;
q[i]=mod;
}
if (sq<0)
{
for(i=0;i<=n-1;i++)
{
q[i]=q[i]*(-1);
}
for(i=0;i<=n-1;i++)
{
if (q[i]==0)
q[i]=1;
else if(q[i]==1)
q[i]=0;
}
for(i=0;i<=n-1;i++)
{
q[i]=q[i]+z[i];
if (q[i]==2)
{
q[i]=0;
q[i+1]=q[i+1]+1;
}
else if (q[i]==3)
{
q[i]=1;
q[i+1]=q[i+1]+1;
}
}
}
display(a,b,q,q1,n);
while (c1!=0)
{
if (q[0]==0&&q1==1)
{
add(a,b,n);
display(a,b,q,q1,n);
}
else
if (q[0]==1&&q1==0)
{
sub(a,b,n);
display(a,b,q,q1,n);
}
q1=q[0];
for(i=0;i<n-1;i++)
{
q[i]=q[i+1];
}
q[n-1]=a[0];
for(i=0;i<n-1;i++)
{
a[i]=a[i+1];
}
c1=c1-1;
display(a,b,q,q1,n);
System.out.println();
}
System.out.print("\nMultiplication is = ");
for(i=n-1;i>=0;i--)
{
System.out.print(a[i]);
}
for(i=n-1;i>=0;i--)
{
System.out.print(q[i]);
}
System.out.println();
System.out.println();
}
catch(Exception e)
{}
}
}
/*
Enter no of bits :: 4
Enter the Multiplicand :: 7
Enter the Multiplier :: 3
A Q Q1 M
0000 0011 0 0111
1001 0011 0 0111
1100 1001 1 0111
1110 0100 1 0111
0101 0100 1 0111
0010 1010 0 0111
0001 0101 0 0111
Multiplication is = 00010101
Process Exit...
*/
Comments
Post a Comment