-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooth's.c
More file actions
127 lines (114 loc) · 3.01 KB
/
Booth's.c
File metadata and controls
127 lines (114 loc) · 3.01 KB
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
#include <stdio.h>
#include <math.h>
int a = 0, b = 0, c = 0, a1 = 0, b1 = 0, com[5] = {1, 0, 0, 0, 0};
int anum[5] = {0}, anumcp[5] = {0}, bnum[5] = {0};
int acomp[5] = {0}, bcomp[5] = {0}, pro[5] = {0}, res[5] = {0};
void binary() {
a1 = fabs(a);
b1 = fabs(b);
int r, r2, i, temp;
for (i = 0; i < 5; i++) {
r = a1 % 2;
a1 = a1 / 2;
r2 = b1 % 2;
b1 = b1 / 2;
anum[i] = r;
anumcp[i] = r;
bnum[i] = r2;
if (r2 == 0) bcomp[i] = 1;
if (r == 0) acomp[i] = 1;
}
c = 0;
for (i = 0; i < 5; i++) {
res[i] = com[i] + bcomp[i] + c;
c = res[i] >= 2 ? 1 : 0;
res[i] %= 2;
}
for (i = 4; i >= 0; i--) {
bcomp[i] = res[i];
}
if (a < 0) {
c = 0;
for (i = 0; i < 5; i++) {
res[i] = com[i] + acomp[i] + c;
c = res[i] >= 2 ? 1 : 0;
res[i] %= 2;
}
for (i = 4; i >= 0; i--) {
anum[i] = res[i];
anumcp[i] = res[i];
}
}
if (b < 0) {
for (i = 0; i < 5; i++) {
temp = bnum[i];
bnum[i] = bcomp[i];
bcomp[i] = temp;
}
}
}
void add(int num[]) {
int i;
c = 0;
for (i = 0; i < 5; i++) {
res[i] = pro[i] + num[i] + c;
c = res[i] >= 2 ? 1 : 0;
res[i] %= 2;
}
for (i = 4; i >= 0; i--) printf("%d", pro[i] = res[i]);
printf(":");
for (i = 4; i >= 0; i--) printf("%d", anumcp[i]);
}
void arshift() {
int temp = pro[4], temp2 = pro[0], i;
for (i = 1; i < 5; i++) pro[i - 1] = pro[i];
pro[4] = temp;
for (i = 1; i < 5; i++) anumcp[i - 1] = anumcp[i];
anumcp[4] = temp2;
printf("\nArithematic Right Shift: ");
for (i = 4; i >= 0; i--) printf("%d", pro[i]);
printf(":");
for (i = 4; i >= 0; i--) printf("%d", anumcp[i]);
}
int main() {
int i, q = 0;
printf("\nEnter two numbers to multiply: ");
do {
printf("\nEnter A: ");
scanf("%d", &a);
printf("Enter B: ");
scanf("%d", &b);
} while (a >= 16 || b >= 16);
printf("\nProduct = %d", a * b);
binary();
printf("\n\nBinary Equivalents are: ");
printf("\nA = ");
for (i = 4; i >= 0; i--) printf("%d", anum[i]);
printf("\nB = ");
for (i = 4; i >= 0; i--) printf("%d", bnum[i]);
printf("\n2's complement = ");
for (i = 4; i >= 0; i--) printf("%d", bcomp[i]);
printf("\n\n");
for (i = 0; i < 5; i++) {
printf("\nPass %d", i + 1);
if (anum[i] == q) {
arshift();
q = anum[i];
} else if (anum[i] == 1 && q == 0) {
printf("\nSubtract B: ");
add(bcomp);
arshift();
q = anum[i];
} else {
printf("\nAdd B: ");
add(bnum);
arshift();
q = anum[i];
}
}
printf("\nProduct is = ");
for (i = 4; i >= 0; i--) printf("%d", pro[i]);
for (i = 4; i >= 0; i--) printf("%d", anumcp[i]);
printf("\n");
return 0;
}