Tuesday, May 21, 2013

Implementation of Binary Division algorithm in c++

Implementation of Binary Division algorithm in C++:

Source code: 

#include<iostream.h>
#include<conio.h>

void add(int &e,int a[],int x[],int n)
{
                int i;
                for(i=0;i<n;i++)
                {              a[i]=a[i]+x[i]+e;
                                if(a[i]>1)
                                {              a[i]=a[i]%2;
                                                e=1;
                                }
                                else
                                                e=0;
                }
}

void complement(int a[],int n)
{              int i,c=0;

                int x[5]={NULL};
                x[0]=1;
                for(i=0;i<n;i++)
                a[i]=(a[i]+1)%2;
               
                add(c,a,x,n);
                cout<<"complement=";
                for(i=n-1;i>=0;i--)
                cout<<a[i];
}

void shl(int &e,int da[],int dq[],int bn)
{              int temp,i;
                temp=dq[bn-1];
                e=da[bn-1];
                for(i=bn-1;i>=0;i--)
                {              da[i]=da[i-1];
                                dq[i]=dq[i-1];
                }
                dq[0]=0;
                da[0]=temp;
}

void display(int da[],int dq[],int bn)
{
                int i;
               
                for(i=bn-1;i>=0;i--)
                cout<<da[i];

                cout<<" ";
                for(i=bn-1;i>=0;i--)
                cout<<dq[i];
}





void main()
{
                int b[5]={NULL},da[5]={NULL},dq[5]={
                NULL},bc[5]={NULL};
                int bn,dn,i,sc,e=0;
                clrscr();
                cout<<"\nEnter the divisor and the dividend";

                cout<<"\nenter the number of bits of the divisor";
                cin>>bn;
                cout<<"\nenter divisor=";
                for(i=bn-1;i>=0;i--)
                cin>>b[i];                          //store the bits of divisor to b[]
                sc=bn;
                for(i=bn-1;i>=0;i--)
                bc[i]=b[i];                         //copy the bits of b[] to bc[] for complement
                complement(bc,bn);              /* calculates the 2's complements of bc[]
                                                                                 and stores in bc[] itself */
                dn=2*bn;
                cout<<"\nThe no. of bits of the dividend(2*no of bits of the divisor)="<<dn;

                cout<<"\ndividend=";
                for(i=bn-1;i>=0;i--)
                cin>>da[i];
                for(i=bn-1;i>=0;i--)
                cin>>dq[i];

                while(sc!=0)
                {             
                                shl(e,da,dq,bn);                                 //shift left operation
                                add(e,da,bc,bn);                              //subtracting da and divisor
                                if(e==1)
                                {              dq[0]=e;
                                }
                                else if(e==0)
                                {              dq[0]=e;
                                                add(e,da,b,bn);              //restoring da by adding divisor
                                }
                                sc--;
                }
               

cout<<"\n\nresult:";
               
                display(da,dq,bn);
                getch();
}

Output:





Friday, May 17, 2013

Implementation of Booth's Algorithm in c++


Multiplication Booth's Algorithm 


#include<iostream.h>
#include<conio.h>

void add(int a[],int x[],int qrn);
void complement(int a[],int n)
{
int i;

int x[8]={NULL};
x[0]=1;
for(i=0;i<n;i++)
{
a[i]=(a[i]+1)%2;
}
add(a,x,n);
}


void add(int ac[],int x[],int qrn)
{
int i,c=0;
for(i=0;i<qrn;i++)
{
ac[i]=ac[i]+x[i]+c;
if(ac[i]>1)
{
ac[i]=ac[i]%2;
c=1;
}
else
c=0;
}

}




void ashr(int ac[],int qr[],int &qn,int qrn)
{
int temp,i;

temp=ac[0];
qn=qr[0];
cout<<"\t\tashr\t\t";
for(i=0;i<qrn-1;i++)
{
ac[i]=ac[i+1];
qr[i]=qr[i+1];
}
qr[qrn-1]=temp;
}

void display(int ac[],int qr[],int qrn)
{ int i;

       for(i=qrn-1;i>=0;i--)

       cout<<ac[i];
       cout<<" ";
       for(i=qrn-1;i>=0;i--)
       cout<<qr[i];


}


void main()
{
clrscr();
int mt[10],br[10],qr[10],sc,ac[10]={0};
int brn,qrn,i,qn,temp;
cout<<"\nEnter the multiplicand and multipier in signed 2's complement form if negative";

cout<<"\nno. of multiplicand bit=";
cin>>brn;
cout<<"\nmultiplicand=";

for(i=brn-1;i>=0;i--)
cin>>br[i]; //multiplicand

for(i=brn-1;i>=0;i--)
mt[i]=br[i];                       // copy multipier to temp array mt[]

complement(mt,brn);


cout<<"\nNo. of multiplier bit=";
cin>>qrn;

sc=qrn;               //sequence counter

cout<<"Multiplier=";
for(i=qrn-1;i>=0;i--)
cin>>qr[i]; //multiplier


qn=0;
temp=0;

cout<<"qn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n";
cout<<"\t\t\tinitial\t\t";
display(ac,qr,qrn);
cout<<"\t\t"<<sc<<"\n";


while(sc!=0)
{       cout<<qr[0]<<"\t"<<qn;
if((qn+qr[0])==1)
{ if(temp==0)
{
add(ac,mt,qrn);
cout<<"\t\tsubtracting BR\t";
for(i=qrn-1;i>=0;i--)
cout<<ac[i];
temp=1;
}
else if (temp==1)
{
add(ac,br,qrn);
cout<<"\t\tadding BR\t";
for(i=qrn-1;i>=0;i--)
cout<<ac[i];
temp=0;
}
cout<<"\n\t";
ashr(ac,qr,qn,qrn);
}
else if(qn-qr[0]==0)
ashr(ac,qr,qn,qrn);

display(ac,qr,qrn);
cout<<"\t";


sc--;
cout<<"\t"<<sc<<"\n";
}
cout<<"Result=";
display(ac,qr,qrn);
       getch();
}


output: