-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbits.h
More file actions
69 lines (50 loc) · 861 Bytes
/
bits.h
File metadata and controls
69 lines (50 loc) · 861 Bytes
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
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
struct bit {
bool v;
};
struct bit bit_from_int(unsigned int x) {
assert(x == 0 || x == 1);
struct bit b;
if (x == 0) {
b.v = false;
} else {
b.v = true;
}
return b;
}
unsigned int bit_to_int(struct bit b) {
if (b.v) {
return 1;
} else {
return 0;
}
}
void bit_print(struct bit b) {
if (b.v) {
putchar('1');
} else {
putchar('0');
}
}
struct bit bit_not(struct bit a) {
struct bit b;
b.v = !a.v;
return b;
}
struct bit bit_and(struct bit a, struct bit b) {
struct bit c;
c.v = a.v && b.v;
return c;
}
struct bit bit_or(struct bit a, struct bit b) {
struct bit c;
c.v = a.v || b.v;
return c;
}
struct bit bit_xor(struct bit a, struct bit b) {
struct bit c;
c.v = ((a.v && !b.v) || (!a.v && b.v));
return c;
}