-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitio.hh
54 lines (44 loc) · 1.45 KB
/
bitio.hh
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
// Eriksen Liu
/*
* A pair of simple classes tu perform stream I/O on individual bits.
*/
#include <iostream>
// BitInput: Read a single bit at a time from an input stream.
// Before reading any bits, ensure your input stream still has valid inputs.
class BitInput
{
public:
// Construct with an input stream
BitInput(std::istream &is);
BitInput(const BitInput &) = default;
BitInput(BitInput &&) = default;
BitInput &operator=(const BitInput &) = default;
BitInput &operator=(BitInput &&) = default;
// Read a single bit (or trailing zero)
// Allowed to crash or throw an exception if called past end-of-file.
bool input_bit();
private:
std::istream ∈ // store istream&
u_int8_t buf; // store a character from the istream&
int nbits; // counter
};
// BitOutput: Write a single bit at a time to an output stream
// Make sure all bits are written out by the time the destructor is done.
class BitOutput
{
public:
// Construct with an input stream
BitOutput(std::ostream &os);
// Flushes out any remaining output bits and trailing zeros, if any:
~BitOutput();
BitOutput(const BitOutput &) = default;
BitOutput(BitOutput &&) = default;
BitOutput &operator=(const BitOutput &) = default;
BitOutput &operator=(BitOutput &&) = default;
// Output a single bit (buffered)
void output_bit(bool bit);
private:
std::ostream &out; // store ostream&
u_int8_t buf; // store one character
int nbits; // counter
};