-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumb-num-bases.c
83 lines (64 loc) · 1.46 KB
/
dumb-num-bases.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
#include <stdio.h>
#include <stdint.h>
#define BITS 3
#define CELL uint8_t
enum CONTAINER_T {
BINARY = 2,
TERNARY = 3,
OCTAL = 8,
DECIMAL = 10,
};
typedef struct CONTAINER {
enum CONTAINER_T tag;
CELL val[BITS];
} container;
void print(container);
container twos(container);
container add(container, container);
void
print(container num)
{
for (int i=0; i<BITS; i++) printf("%d", num.val[i]);
}
container
twos(container num)
{
container one = { .tag = num.tag, .val = {[BITS-1]=1} };
container result = { .tag = num.tag };
for (int i=0; i<BITS; i++)
result.val[i] = num.tag - num.val[i] - 1;
return add(result, one);
}
container
add(container lvalue, container rvalue)
{
if (lvalue.tag != rvalue.tag) {
puts("Type error: invalid addition of different bases");
return (container){0};
}
container result = { .tag = lvalue.tag };
CELL *o, *l, *r, carry = 0;
o = result.val;
l = lvalue.val;
r = rvalue.val;
for (int i = BITS-1; i>-1; i--) {
o[i] = l[i] + r[i] + carry;
carry = 0;
if (o[i] >= result.tag) {
carry = o[i] / result.tag;
o[i] = o[i] % result.tag;
}
}
return result;
}
int main(int argc, char* argv[])
{
container l = { .tag = DECIMAL, .val = {[BITS-3] = 5, 0, 0} };
container r = { .tag = DECIMAL, .val = {[BITS-3] = 3, 2, 9} };
/* l = twos(l); // twos 500(10) -> 500 */
/* r = twos(r); // twos 329(10) -> -329 */
print(l); printf(" + "); print(r); printf(" = "); print(
add(l, r)
); puts("");
return 0;
}