-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiply.c
68 lines (48 loc) · 799 Bytes
/
multiply.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
#include "stdio.h"
#include "stdlib.h"
int multiply_i(int a, int b)
{
int i = 1, cn = 0, left = 0, rst = a;
while (i < b)
{
i <<= 1;
++cn;
}
left = i - b;
while (cn > 0)
{
rst <<= 1;
--cn;
}
while (left > 0)
{
rst -= a;
--left;
}
return rst;
}
int multiply_r(int a, int b)
{
int c = 0, d = 0, ret = 0;
if (b == 1)
return a;
c = b >> 1;
d = b - (c << 1);
ret = multiply_r(a << 1, c) + ((d) ? a : 0);
printf("a %d b %d c %d d %d ret %d\n", a, b, c, d, ret);
return ret;
}
int multiply(int x, int y)
{
if ((x == 0) || (y == 0))
return 0;
if (x < y)
return multiply_r(y, x);
return multiply_r(x, y);
}
int main(int argc, char *argv[])
{
int x = 123, y = 456;
printf("r %d i %d (%d)\n", multiply(x, y), multiply_i(x, y), x * y);
return 0;
}