You are on page 1of 11

//C++ Code to perform 2s Complement Division //Code written by Ajitabh Gupta #include<iostream> #include<cmath> using namespace std; void

dectobin(int*,int); int bintodec(int*,int); int n; int main() { int a,b,sc,ssor,send,s1,c,sum,allz,rem,quo; char ch; start: cout<<"Enter dividend in decimal format\n"; cin>>a; cout<<"Enter divisor in decimal format\n"; cin>>b; if(a!=0 && b==0) cout<<"Infinity\n"; //Handling Divide-by-Zero Exception else if(a==0 && b==0) cout<<"Undeterminate\n"; //Handling case when both dividend and divisor are zero else { if(a<0 && b<0)

{ a=abs(a); b=abs(b); cout<<"As both dividend and divisor are negative, -ve sign cancels out\n"; } if(a<0 && b>0) { a=abs(a); b*=-1; } float td; td=log((double)abs(a))/log(2.0); n=(int)ceil(td+1); //Finding out the number of digits required to store both dividend and divisor in binary format cout<<"\nSC is "<<n<<endl; sc=n; int Q[n],A[n],M1[n],M2[n],pra[n],i; dectobin(Q,a); //Converting the dividend into binary as Q cout<<"\nQ is "; for(i=0;i<=n-1;i++) cout<<Q[i]; send=Q[0]; dectobin(M1,b); //Converting the divisor into binary as M1 cout<<"\nM is ";

for(i=0;i<=n-1;i++) cout<<M1[i]; ssor=M1[0]; for(i=n-1;i>=0;i--) //Taking the 2's Complement of M1 and storing it as M2 { if(M1[i]==0) M2[i]=0; else break; } M2[i--]=1; while(i>=0) M2[i]=!M1[i--]; cout<<"\nM'+1 is "; for(i=0;i<=n-1;i++) cout<<M2[i]; int AQ[2*n]; //Creating a combined array of A and Q named AQ for(i=0;i<n;i++) AQ[i]=0; for(i=n;i<2*n;i++) AQ[i]=Q[i-n]; cout<<"\nInitial AQ : "; for(i=0;i<=n-1;i++) cout<<AQ[i];

cout<<" "; for(i=n;i<=2*n-1;i++) cout<<AQ[i]; cout<<endl; while(sc-->0) //Repeating the 2's Complement Division Algorithm SC number of times { cout<<"\nAQ : "; //Displaying current AQ for(i=0;i<=n-1;i++) cout<<AQ[i]; cout<<" "; for(i=n;i<=2*n-1;i++) cout<<AQ[i]; cout<<" SC is "<<sc+1; for(i=0;i<=(2*n-2);i++) //Shifting AQ Left by 1 bit AQ[i]=AQ[i+1]; AQ[2*n-1]=0; cout<<"\nShift Left "; //Displaying Left Shifted AQ cout<<"AQ : "; for(i=0;i<=n-1;i++) cout<<AQ[i]; cout<<" "; for(i=n;i<=2*n-1;i++) cout<<AQ[i]; s1=AQ[0]; //Storing the original sign of A

for(i=0;i<n;i++) pra[i]=AQ[i]; //Storing the previous value of A to restore later if required if(AQ[0]==M1[0]) //Checking if A and M have the same sign { cout<<"\nAdd M'+1 "; for(i=0;i<=n-1;i++) cout<<M2[i]; c=0; for(i=n-1;i>=0;i--) { sum=c+AQ[i]+M2[i]; //Adding 2's Complement of M to A switch(sum) { case 0: {c=0;AQ[i]=0;break;} case 1: {c=0;AQ[i]=1;break;} case 2: {c=1;AQ[i]=0;break;} case 3: {c=1;AQ[i]=1;break;} default: {cout<<"\nError Encountered!";break;} } } cout<<"\nAQ: "; //Displaying AQ after the addition for(i=0;i<=n-1;i++) cout<<AQ[i]; cout<<" ";

for(i=n;i<=2*n-1;i++) cout<<AQ[i]; } else //Executed if A and M have different signs { cout<<"\nAdd M "; for(i=0;i<=n-1;i++) cout<<M1[i]; c=0; for(i=n-1;i>=0;i--) { sum=c+AQ[i]+M1[i]; //Adding M to A switch(sum) { case 0: {c=0;AQ[i]=0;break;} case 1: {c=0;AQ[i]=1;break;} case 2: {c=1;AQ[i]=0;break;} case 3: {c=1;AQ[i]=1;break;} default: {cout<<"\nError Encountered!";break;} } } cout<<"\nAQ: "; //Displaying AQ after the addition for(i=0;i<=n-1;i++) cout<<AQ[i];

cout<<" "; for(i=n;i<=2*n-1;i++) cout<<AQ[i]; } allz=1; //Checking if A is equal to zero or not for(i=n-1;i>=0;i--) if(AQ[i]==1) { allz=0; break; } if(s1==AQ[0] || allz==1) //Executed if sign of A is the same before and after the operation OR if A is zero { AQ[2*n-1]=1; //Setting Q0 equal to 1 cout<<"\nSet Q0=1 \n"; } if(s1!=AQ[0] && allz==0) //Executed if sign of A is different before and after the operation AND A is non-zero { AQ[2*n-1]=0; //Setting Q0 equal to 0 cout<<"\nSet Q0=0 and Restore A \n"; for(i=0;i<n;i++) //Restoring the previous value of A AQ[i]=pra[i]; }

} if(send!=ssor) //Checking if the sign of dividend is different from the divisor's sign { for(i=2*n-1;i>=n;i--) if(AQ[i]==1) break; i--; while(i>=n) AQ[i]=!AQ[i--]; } for(i=0;i<n;i++) { A[i]=AQ[i]; Q[i]=AQ[i+n]; } rem=bintodec(A,n); //Converting A from binary to decimal format, A is the remainder after division quo=bintodec(Q,n); //Converting Q from binary to decimal format, Q is the quotient of the division cout<<"\nQuotient is "<<quo<<" and Remainder is "<<rem; //Displaying the Quotient and Remainder } cout<<"\nPress Y to perform another division or any other key to exit!\n"; cin>>ch;

if(ch=='Y' || ch=='y') //Asking the user if he wants to carry out another division or exit the program goto start; return 0; } void dectobin(int *arr,int num) //Function to convert the passed decimal number into binary and return the base address of the binary array { int neg,i=0,j,temp; if(num<0) //Checking for a negative number { neg=1; num=abs(num); } while(num!=0) //Continuously dividing the number by 2 and storing the remainder { *(arr+i++)=num%2; num/=2; } for(j=i;j<=n-1;j++) *(arr+j)=0; for(i=0;i<=(n-1)/2;i++) //Reversing the binary array { temp=*(arr+i);

*(arr+i)=*(arr+n-1-i); *(arr+n-1-i)=temp; } if(neg==1) { *arr=1; //Setting sign bit to 1 in case of a negative number for(i=n-1;i>=1;i--) //Taking 2's Complement to store the negative number if(*(arr+i)==1) break; i--; while(i>=1) *(arr+i)=!*(arr+i--); } } int bintodec(int *p,int y) //Function to convert a binary array to decimal by taking its base address as input and returning the decimal equivalent { int sum=0,i; if(*p==1) //Taking 2's Complement of the negative number to get back its original magnitude { for(i=n-1;i>=1;i--) if(*(p+i)==1) break;

i--; while(i>=1) *(p+i)=!*(p+i--); } for(i=1;i<=y-1;i++) //Repeatedly multiplying by increasing powers of 2 and storing the sum after each iteration sum+=((int)pow(2.0,y-1-i)*(*(p+i))); if(*p==1) sum*=-1; return sum; //Returning the equivalent decimal of the passed binary array }

You might also like