-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivisionAlgorithm.java
116 lines (116 loc) · 2.65 KB
/
DivisionAlgorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class DivisionAlgorithm
{
public static int q=0;
public static int r=0;
private static byte[] registerM = new byte[Main.n];
private static byte[] registerQ = new byte[Main.n];//q
private static byte[] registerA = new byte[Main.n];//r
private static byte[] negative = new byte[Main.n];
private static void initialiseRegisters(byte[] dividend, byte[] divisor)
{
// Negative
for(byte i=0;i<divisor.length;i++)
negative[i] = divisor[i];
Main.optimisedTwosComplement(negative,negative.length);
// M
Main.clearRegister(registerM);
for(byte i=0;i<divisor.length;i++)
registerM[i] = divisor[i];
// S
Main.clearRegister(registerQ);
for(byte i=0;i<dividend.length;i++)
registerQ[i] = dividend[i];
//A
Main.clearRegister(registerA);
}
public static boolean isZero(byte[] x)
{
for(int i=0;i<x.length;i++)
if(x[i]==1)
return false;
return true;
}
public static void divide(byte[] dividend,byte[] divisor)
{
int flag=0;
int f=0;
if(isZero(divisor))
{
System.out.println("divisor is zero");
return;
}
if(dividend[0]==1)
{
Main.optimisedTwosComplement(dividend,dividend.length );
flag=1;
}
if(divisor[0]==1)
{
Main.optimisedTwosComplement(divisor,divisor.length);
f=1;
}
initialiseRegisters(dividend,divisor);
int size=Main.n;
while(size>0)
{
byte t=registerA[0];
shiftLeft();
if(t==1)
Main.add(registerA,registerM);
else
Main.add(registerA,negative);
t=registerA[0];
if(t==1)
registerQ[registerQ.length-1]=0;
else
registerQ[registerQ.length-1]=1;
size--;
}
if(registerA[0]==1)
Main.add(registerA,registerM);
r=Main.decimal(registerA,registerA.length);
if(flag==1)
Main.optimisedTwosComplement(dividend,dividend.length);
if(f==1)
Main.optimisedTwosComplement(divisor,divisor.length);
if((flag==1&&f==0)||(flag==0&&f==1))
Main.optimisedTwosComplement(registerQ,registerQ.length);
if(((registerQ[0]==1&&f==0)||(registerQ[0]==0&&f==1))&&!isZero(registerQ))
{
Main.optimisedTwosComplement(registerA,registerA.length);
r=-r;
}
else if(flag==1)
{
Main.optimisedTwosComplement(registerA,registerA.length);
r=-r;
}
q=Main.decimal(registerQ,registerQ.length);
}
public static String getr()
{
return Main.getString(registerA,registerA.length);
}
public static String getq()
{
return Main.getString(registerQ,registerQ.length);
}
//shift left AQ
private static void shiftLeft()
{
byte a=registerQ[0];
for(int i=registerA.length-1;i>=0;i--)
{
byte temp=registerA[i];
registerA[i]=a;
a=temp;
}
a=0;
for(int i=registerQ.length-1;i>=0;i--)
{
byte temp=registerQ[i];
registerQ[i]=a;
a=temp;
}
}
}