Booth
Booth
#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}, bcomp[5] = {0}, pro[5] =
{0}, res[5] = {0};
void binary() {
a1 = fabs(a); b1 = fabs(b); int r, r2, i;
for (i = 0; i < 5; i++) {
r = a1 % 2; a1 /= 2; r2 = b1 % 2; b1 /= 2;
anum[i] = r; anumcp[i] = r; bnum[i] = r2; bcomp[i] = r2 ? 0 : 1;
}
c = 0; // two's complement for b
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] + !anum[i] + c, c = res[i] >= 2 ? 1 : 0,
res[i] %= 2;
for (i = 4; i >= 0; i--) anum[i] = anumcp[i] = res[i];
}
void arshift() {
int temp = pro[4], temp2 = pro[0], i;
for (i = 1; i < 5; i++) pro[i - 1] = pro[i], anumcp[i - 1] = anumcp[i];
pro[4] = temp; anumcp[4] = temp2;
printf("\nAR-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("\tBOOTH'S MULTIPLICATION ALGORITHM\nEnter A and B (<16):
");
do { scanf("%d %d", &a, &b); } while (a >= 16 || b >= 16);