-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestoring_division.cpp
145 lines (126 loc) · 3.48 KB
/
restoring_division.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <bits/stdc++.h>
using namespace std;
string decimalToBinary(int n) {
string curr="";
if(n==0) return "0";
while(n>0) {
curr = to_string(n%2) + curr;
n=n/2;
}
return curr;
}
string twosComplement(string M) {
string twos;
for(int i=0;i<M.length();i++) {
if(M[i]=='0')
twos+=to_string(1);
else
twos+=to_string(0);
}
for(int i=twos.length()-1;i>=0;i--) {
if(twos[i]=='1') twos[i]='0';
else {
twos[i]='1';
break;
}
}
return twos;
}
vector<string> shiftLeft(string C, string AC, string Q) {
C = AC[0];
for(int i=0;i<AC.length()-1;i++) AC[i] = AC[i+1];
AC[AC.length()-1]=Q[0];
for(int i=0;i<Q.length()-1;i++) Q[i] = Q[i+1];
Q[Q.length()-1]='X';
return {C, AC, Q};
}
string addition(string C, string AC, string M) {
string curr="";
string foradd = C+AC;
int carry=0;
for(int i=M.length()-1;i>=0;i--) {
if(carry==1 && foradd[i]=='1' && M[i]=='1') curr = "1"+curr;
else if(carry==1 && (foradd[i]=='1' || M[i]=='1')) curr = "0"+curr;
else if(carry==1) {
curr = "1"+curr;
carry=0;
}
else if(foradd[i]=='1' && M[i]=='1') {
curr = "0"+curr;
carry=1;
}
else if(foradd[i]=='1' || M[i]=='1') {
curr = "1"+curr;
}
else curr = "0"+curr;
}
return curr;
}
int main()
{
int ac=0, m, q, count;
cout << "Enter divident: ";
cin >> q;
cout << "Enter Divisor: ";
cin >> m;
cout << "C\tAC\tQ\t\tOperation" << endl;
cout << "-------------------------------------------" << endl;
string Q = decimalToBinary(q);
string M = decimalToBinary(m);
int mlen=M.length();
for(int i=0;i<=(Q.length()-mlen);i++) M = "0"+M;
string not_M = twosComplement(M);
string AC;
string C = "0";
for(int i=0;i<Q.length();i++) AC=AC+"0";
cout << C << "\t";
cout << AC << "\t";
cout << Q << "\t\t";
cout << "Initialization" << endl << endl;
count = Q.length();
vector<string> v;
v.push_back(C);
v.push_back(AC);
v.push_back(Q);
bool flag=false;
while(count>0) {
cout << "-----------------Cycle--------------------" << endl;
v = shiftLeft(v[0], v[1], v[2]);
cout << v[0] << "\t";
cout << v[1] << "\t";
cout << v[2] << "\t\t";
cout << "Left Shift" << endl;
string pstadd;
if(!flag) {
pstadd = addition(v[0], v[1], not_M);
cout << pstadd[0] << "\t";
cout << pstadd.substr(1) << "\t";
cout << v[2] << "\t\t";
cout << "AC = AC-M" << endl;
} else {
pstadd = addition(v[0], v[1], M);
cout << pstadd[0] << "\t";
cout << pstadd.substr(1) << "\t";
cout << v[2] << "\t\t";
cout << "AC = AC+M" << endl;
}
cout << pstadd[0] << "\t";
cout << pstadd.substr(1) << "\t";
if(pstadd[0]=='0') {
flag=false;
v[2][Q.length()-1]='1';
cout << v[2] << "\t\t";
cout << "Q0-->1" << endl << endl;
}
else {
flag=true;
v[2][Q.length()-1]='0';
cout << v[2] << "\t\t";
cout << "Q0-->0" << endl << endl;
}
v[0]=pstadd[0];
v[1]=pstadd.substr(1);
count--;
}
return 0;
}