You are on page 1of 5

COMPUTER ARCHITECTURE AND ORGANIZATION (ITE-2001)

THEORY DIGITAL ASSIGNMENT -02


B1+TB1
ABHISHEK K B
20BIT0403
INFORMATION TECHNOLOGY

-Booth Multiplication

Implementation of binary division (non-restoring method) using java code:

IDE: Intellij
CODE:
import java.util.*;
public class division{
public static void main(String args[]){
String m="",n="";
int N;
StringBuilder A=new StringBuilder();
StringBuilder M=new StringBuilder();
StringBuilder Q=new StringBuilder();
StringBuilder AQ=new StringBuilder();
int ST;
Scanner s=new Scanner(System.in);
System.out.println("1- binary number format\n2-decimal number
format");
ST=s.nextInt();
if(ST==1){
System.out.println("Enter DIVIDEND");
m=s.next();
System.out.println("Enter DIVISOR");
n=s.next();
}
else if(ST==2){
System.out.println("Enter DIVIDEND");
System.out.println("Enter DIVISOR");
m=s.next();
n=s.next();
int r1,r2;
r1=Integer.parseInt(m);
r2=Integer.parseInt(n);
m=Integer.toBinaryString(r1);
n=Integer.toBinaryString(r2);
}
for(int i=0;i<m.length()+1;i++)
A.append("0");
M.append(n);
Q.append(m);
N=Q.length();
AQ.append(A);
AQ.append(Q);
StringBuilder MEA=new StringBuilder(AQ);
int na;
int PQR=0;
System.out.println("INIT A="+A+" Q="+Q+" AQ="+AQ+" N="+N+" M "+M+"
N "+PQR);
for(int i=0;i<N;i++){
for(int j=0;j<AQ.length()-1;j++){ //shift
String x=MEA.substring(j+1,j+2);
AQ.replace(j,j+1,x);
}
AQ.replace(AQ.length()-1,AQ.length(),"0");
System.out.println("SHL N="+(i+1)+" AQ="+AQ);
StringBuilder RS=new
StringBuilder(AQ.substring(0,m.length()+1)); //A
if(RS.substring(0,1).compareTo("1")==0){

na=Integer.parseInt(AQ.substring(0,m.length()+1),2)+Integer.parseInt(n,2);
StringBuilder nal=new
StringBuilder(Integer.toBinaryString(na));
if(nal.length()<RS.length()){
String tnal=new String(nal);
for(int z=0;z<=RS.length()-nal.length();z++){
RS.replace(z,z+1,"0");}
RS.replace(RS.length()-nal.length(),RS.length(),tnal);
String ta=new String(RS);
RS.replace(nal.length(),RS.length(),tnal);
AQ.replace(0,RS.length(),ta);
System.out.println("A>0 A=A+M AQ="+AQ+" A="+RS);
}else{
String tnal=new String(nal.substring(nal.length()-
RS.length(),nal.length()));
RS.replace(0,RS.length(),tnal);
String ta=new String(RS);
AQ.replace(0,RS.length(),ta);
System.out.println("A<0 A=A+M AQ="+AQ+" A="+RS);
}}
else if(RS.substring(0,1).compareTo("0")==0){
na=Integer.parseInt(AQ.substring(0,m.length()+1),2)-
Integer.parseInt(n,2);
StringBuilder nal=new
StringBuilder(Integer.toBinaryString(na));
if(nal.length()<RS.length()){
String tnal=new String(nal);
for(int z=0;z<=RS.length()-nal.length();z++){
RS.replace(z,z+1,"0");}
RS.replace(RS.length()-nal.length(),RS.length(),tnal);
String ta=new String(RS);
AQ.replace(0,RS.length(),ta);
System.out.println("A>0 A=A-M AQ="+AQ+" A="+RS);
}else{
String tnal=new String(nal.substring(nal.length()-
RS.length(),nal.length()));
RS.replace(0,RS.length(),tnal);
String ta=new String(RS);
AQ.replace(0,RS.length(),ta);
System.out.println("A>0 A=A-M AQ="+AQ+" A="+RS);}
}
if(RS.substring(0,1).compareTo("1")==0){
AQ.replace(AQ.length()-1,AQ.length(),"0");
System.out.println("A<0 Q0=0 AQ="+AQ+" A="+RS);
}
else if(RS.substring(0,1).compareTo("0")==0){
AQ.replace(AQ.length()-1,AQ.length(),"1");
System.out.println("A>0 Q0=1 AQ="+AQ+" A="+RS);
}

String RT=new String(AQ);


MEA.replace(0,MEA.length(),RT);
System.out.println("\n");

}
A.replace(0,A.length(),AQ.substring(0,m.length()+1));
Q.replace(0,Q.length(),AQ.substring(m.length()+1));
if(A.substring(0,1).compareTo("1")==0){
for(int q=0;q<A.length();q++)
{if(A.substring(q,q+1).compareTo("0")==0){
A.replace(q,q+1,"1");
}
else if(A.substring(q,q+1).compareTo("1")==0){
A.replace(q,q+1,"0");}}
String A1=new String(A);
int tt=Integer.parseInt(A1,2)+1;
String A2=new String(Integer.toBinaryString(tt));
int temp=Integer.parseInt(n,2)-Integer.parseInt(A2,2);
A.replace(0,A.length(),Integer.toBinaryString(temp));
if(temp==0){
A.replace(0,A.length(),"0");
}

}
String q=new String(Q);
String r=new String(A);
int quotient=Integer.parseInt(q,2);
int remainder=Integer.parseInt(r,2);
System.out.println("binary\nreminder=" +r+"\n"+"Quotient="+q);
System.out.println("Decimal\nreminder= "+remainder+"\n"+"Quotient=
"+quotient);

}
}

output:

You might also like