-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHamming.h
43 lines (31 loc) · 1.2 KB
/
Hamming.h
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
// Hamming.h
// author: @mikesoylu
#ifndef HAMMING_H
#define HAMMING_H
#define IS_POWER_OF_TWO(x) (!((x) & ((x) - 1)) && (x))
class Hamming {
private:
int packet_length;
char *packet;
char *dest;
static bool getBit(unsigned char byte, int bit);
static void setBit(char &byte, int bit, bool value);
// gets the value of a parity bit at position bit which IS_POWER_OF_TWO
bool getParityBit(char *data, int bit, int data_bits);
public:
Hamming(char *packet, char *dest, int packet_length):
packet(packet), dest(dest), packet_length(packet_length) { };
// encodes packet of length getEncodedLength to dest
// dest should be alloc'd before calling.(use getEncodedLength)
void encode();
// decodes a packet of length getDecodedLength to dest
// dest should be alloc'd before calling.(use getDecodedLength)
void decode();
// returns length of a packet when encoded
static int getEncodedLength(int packet_length);
// returns length of a packet when decoded
static int getDecodedLength(int packet_length);
// unit tests
static bool test();
};
#endif