-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitio.cc
63 lines (57 loc) · 1.52 KB
/
bitio.cc
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
// Eriksen Liu
#include "bitio.hh"
#include <iostream>
using namespace std;
BitInput::BitInput(std::istream &is)
: in(is), buf(0), nbits(8) // initialize the variables. (nbits = 8 because we what it to get a charactor at first)
{
}
bool BitInput::input_bit()
{
// if we shift the buffer 8 times, then get a new character from in and store it in buffer, reset the counter (nbits)
if (nbits == 8)
{
buf = in.get();
nbits = 0;
}
// shift buffer right (7-nbits) to get the most siginificent figure (don't change the value of buffer), and with one to get its bool value
bool val;
val = buf >> (7 - nbits) & 1;
// increase the counter (nbits)
nbits++;
// return the boolean value
return val;
}
BitOutput::BitOutput(std::ostream &os)
: out(os), buf(0), nbits(0) // initialize the variables
{
}
void BitOutput::output_bit(bool bit)
{
// if we added 8 bits in the buffer, then put the buffer into out and reset buffer and counter (nbits)
if (BitOutput::nbits == 8)
{
out.put(buf);
buf = 0;
nbits = 0;
}
// shift left one time to make space to store bit (new space always fills with a 0)
buf = buf << 1;
// or with bit to change the value of the last bit of buffer
buf = buf | bit;
// increase the counter
nbits++;
return;
}
BitOutput::~BitOutput()
{
// if there are still spaces for buffer, then fill them then put the buffer into output
while (nbits != 8)
{
// filled with trailing zeros
buf = buf << 1;
nbits++;
}
// put the extra buffer into out.
out.put(buf);
}