-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.c
59 lines (49 loc) · 1.04 KB
/
code.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
#include "code.h"
#include "defines.h"
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Code code_init(void) {
Code c;
c.top = 0;
for (uint32_t i = 0; i < MAX_CODE_SIZE; i++) {
c.bits[i] = 0;
}
return c;
}
uint32_t code_size(Code *c) {
return c->top;
}
bool code_empty(Code *c) {
return c->top == 0;
}
bool code_full(Code *c) {
return c->top == MAX_CODE_SIZE;
}
bool code_push_bit(Code *c, uint8_t bit) {
if (code_full(c)) {
return false;
}
c->bits[c->top / 8] |= (bit << (c->top % 8));
c->top++;
return true;
}
bool code_pop_bit(Code *c, uint8_t *bit) {
if (code_empty(c)) {
return false;
}
c->top--;
*bit = (c->bits[c->top / 8] >> (c->top % 8) & 0x1);
c->bits[c->top / 8] &= ~(0x1 << (c->top % 8));
return true;
}
void code_print(Code *c) {
for (uint32_t i = 0; i < c->top; i++) {
printf("%d", (c->bits[i / 8] >> (i % 8) & 0x1));
}
printf("\n");
return;
}