-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitwriter.cpp
54 lines (48 loc) · 967 Bytes
/
bitwriter.cpp
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
#ifndef _BITWRITER_CPP_
#define _BITWRITER_CPP_
#include "bitwriter.h"
void bitwriter::write_n_th_bit(bool val, int _ind1, int _ind2)
{
if (_ind1 == -1)
_ind1 = ind1;
if (_ind2 == -1)
_ind2 = ind2;
uint8_t &ch = buffer[_ind1];
uint8_t mask = (1 << _ind2);
if (val)
ch |= mask;
else if (_ind2 < sizeof(uint8_t)*8)
ch &= ~mask;
}
bool bitwriter::get_n_th_bit(int _ind1, int _ind2) const
{
if (_ind1 == -1)
_ind1 = ind1;
if (_ind2 == -1)
_ind2 = ind2;
uint8_t ch = buffer[_ind1];
uint8_t mask = (1 << _ind2);
return (ch & mask) != 0;
}
void bitwriter::write_next_bit(bool val)
{
write_n_th_bit(val);
++ind2;
if (ind2 == 8)
{
buffer[++ind1] = 0;
ind2 = 0;
}
}
bool bitwriter::get_next_bit() const
{
bool result = get_n_th_bit();
++ind2;
if (ind2 == 8)
{
++ind1;
ind2 = 0;
}
return result;
}
#endif