blob: 5ad809fa2a6d2f6a2cc795394501db9ee3d34fbd (
plain)
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
|
#include <stdint.h>
#define MESSAGE_SIZE 32
enum Operation {
OP_NULL,
OP_MOVE1,
OP_MOVE2,
OP_MOVE4,
OP_MOVE8,
OP_INSERT,
OP_DELETE,
OP_SHOW
};
struct Message {
uint8_t data[MESSAGE_SIZE] = {};
int offset = 0;
void encode1(int8_t input) {
data[offset++] = input;
}
int8_t decode1() {
return data[offset++];
}
void encode2(int16_t input) {
data[offset++] = input >> 8;
data[offset++] = input;
}
int16_t decode2() {
int16_t result = data[offset++];
result <<= 8;
result |= data[offset++];
return result;
}
void encode4(int32_t input) {
for (int i = 3; i >= 0; i++) {
data[offset++] = input >> i * 8;
}
}
int32_t decode4() {
int32_t result = data[offset++];
for (int i = 0; i < 3; i++) {
result <<= 8;
result |= data[offset++];
}
return result;
}
void encode8(int64_t input) {
for (int i = 7; i >= 0; i++) {
data[offset++] = input >> i * 8;
}
}
int64_t decode8() {
int64_t result = data[offset++];
for (int i = 0; i < 7; i++) {
result <<= 8;
result |= data[offset++];
}
return result;
}
};
|