-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.c
75 lines (64 loc) · 1.37 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "code.h"
#include "defines.h"
#include <stdbool.h>
#include <stdint.h>
// Description:
// Checks if a code is empty.
//
// Parameters:
// Code *c - The code to check.
//
// Returns:
// bool - Whether the code is empty.
bool code_empty( Code *c ) {
return c->top == 0;
}
// Description:
// Checks if a code is full.
//
// Parameters:
// Code *c - The code to check.
//
// Returns:
// bool - Whether the code is full.
bool code_full( Code *c ) {
return c->top == MAX_CODE_SIZE * 8;
}
// Description:
// Pushes a bit to a code.
//
// Parameters:
// Code *c - The code to push to.
// uint8_t bit - The bit to push to the code.
//
// Returns:
// bool - Whether the bit was pushed successfully.
bool code_push_bit( Code *c, uint8_t bit ) {
if ( code_full( c ) ) {
return false;
}
if ( bit == 0 ) { // Clear bit.
c->bytes[ c->top / 8 ] &= ~( 1 << ( c->top % 8 ) );
} else { // Set bit.
c->bytes[ c->top / 8 ] |= ( 1 << ( c->top % 8 ) );
}
c->top++;
return true;
}
// Description:
// Pops a bit from a code.
//
// Parameters:
// Code *c - The code to pop from..
// uint8_t *bit - The pointer to a uint8_t to set to the popped bit.
//
// Returns:
// bool - Whether the bit was popped successfully.
bool code_pop_bit( Code *c, uint8_t *bit ) {
if ( code_empty( c ) ) {
return false;
}
c->top--;
*bit = ( 1 & ( c->bytes[ c->top / 8 ] >> ( c->top % 8 ) ) );
return true;
}