-
Notifications
You must be signed in to change notification settings - Fork 1
/
division.c
268 lines (266 loc) · 7.48 KB
/
division.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include<stdio.h>
#include<math.h>
void add(int A[], int B[],int n){ //function to add the binary number
int carry=0,res[n+1];
for (int i=0;i<=n;i++){
res[i] =A[i] + B[i] + carry;
if (res[i] >= 2){
carry = 1;
}
else{
carry= 0;
}
res[i] = res[i]%2;
}
for (int i=n;i>=0;i--){
A[i]=res[i];
}
}
void shift_left(int A[],int Q[],int n){ //function to arithmetic shift for the A, Q
int temp=Q[n];
for(int i=n;i>0;i--){
Q[i]=Q[i-1];
}
for(int i=n;i>0;i--){
A[i]=A[i-1];
}
A[0]=temp;
}
int main(){
int a, a1 ,b, b1, n, flag_a=0, flag_b=0, sign_a=1, sign_b=1, choice;
printf("Enter bit-size: ");
scanf("%d",&n);
printf("Enter Dividend: ");
scanf("%d", &a); //Dividend
printf("Enter Divisor: ");
scanf("%d", &b);
n=n-1; //Divisor
if(a<0){ //Absolute value of Dividend
a1=abs(a);
flag_a=1;
sign_a=-1;
}
else{
a1=a;
}
if(b<0){ //Absolute value of divisor
b1=abs(b);
flag_b=1;
sign_b=-1;
}
else{
b1=b;
}
// if (a1<=b1){ //bitsize
// n=(int)log2(b1)+2;
// }
// else{
// n=(int)log2(a1)+2;
// }
int M[n+1], Mcomp[n+1], Q[n+1], A[n+1], q=0;
for (int i=0;i<=n;i++){ //making binary
M[i]=b1%2;
Q[i]=a1%2;
A[i]=0;
a1=a1/2;
b1=b1/2;
}
int carry=1; //M compliment for subtracting
for(int i=0;i<=n;i++){
if (M[i]==1){
Mcomp[i]=0;
}
else{
Mcomp[i]=1;
}
}
for(int i=0;i<=n;i++){
if ((Mcomp[i]==1)&&carry==1){
Mcomp[i]=0;
}
else if ((Mcomp[i]==0)&&carry==1){
Mcomp[i]=1;
carry=0;
}
}
printf("\n");
printf("Binary form of Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\nBinary form of M: ");
for (int i=n;i>=0;i--){
printf("%d",M[i]);
}
printf("\nBinary form of M': ");
for (int i=n;i>=0;i--){
printf("%d",Mcomp[i]);
}
printf("\n\nEnter Your Choice\n");
printf(" 1 for Restoring Division algorithm\n");
printf(" 2 for Non-Restoring Division algorithm\n");
scanf("%d",&choice);
printf("\n\n");
if(choice==1){ //restoring division algorithm
printf("******Restoring Division algorithm******\n\n");
for (int i=0;i<=n;i++){
shift_left(A,Q,n);
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tLeft Shift\n");
add(A,Mcomp,n);
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tA <-- A-M\n");
if (A[n]==1){
Q[0]=0;
add(A,M,n);
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tA <-- A+M & Set Q[0]=1\n");
}
else{
Q[0]=1;
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tSet Q[0]=1\n");
}
printf("\n");
}
printf("\n");
}
else if(choice==2){ //non-restoring division algorithm
printf("******Non-Restoring Division algorithm******\n\n");
for (int i=0;i<=n;i++){
shift_left(A,Q,n);
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tLeft Shift\n");
if (A[n]==1){
add(A,M,n);
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tA <-- A+M\n");
}
else{
add(A,Mcomp,n);
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tA <-- A-M\n");
}
if (A[n]==1){
Q[0]=0;
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tSet Q[0]=0\n\n");
}
else{
Q[0]=1;
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tSet Q[0]=1\n\n");
}
}
if(A[n]==1){
add(A,M,n);
printf("A: ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\t");
printf("Q: ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
printf("\tA <-- A+M\n");
}
}
else{
printf("Enter a valid Choice");
getch();
return 0;
}
printf("A(Remainder): ");
for (int i=n;i>=0;i--){
printf("%d",A[i]);
}
printf("\n");
printf("Q(Quotient) : ");
for (int i=n;i>=0;i--){
printf("%d",Q[i]);
}
int rem=0, quo=0;
for(int i=n;i>=0;i--){
rem+=A[i]*pow(2,i);
}
for(int i=n;i>=0;i--){
quo+=Q[i]*pow(2,i);
}
printf("\nRemainder in Decimal after sign correction: %d", rem*sign_a);
printf("\nQuoitent in Decimal after sign correction : %d\n", quo*sign_a*sign_b);
getch();
return 0;
}