-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitio.hh
More file actions
32 lines (26 loc) · 998 Bytes
/
bitio.hh
File metadata and controls
32 lines (26 loc) · 998 Bytes
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
/*
* A simple class to perform stream I/O on individual bits.
* Before reading any bits, ensure your input stream still has valid inputs.
*/
#include <iostream>
class BitIO {
public:
// Construct with one of either an input stream or output (the other null)
BitIO(std::ostream* os, std::istream* is);
// Flushes out any remaining output bits and trailing zeros, if any:
~BitIO();
BitIO(const BitIO&) = default;
BitIO(BitIO&&) = default;
BitIO& operator=(const BitIO&) = default;
BitIO& operator=(BitIO&&) = default;
// Output a single bit (buffered)
void output_bit(bool bit);
// Read a single bit (or trailing zero)
bool input_bit();
private:
int bit_index_; // tells us which bit we are currently pointing at in buffer
char buffer_; // tempoarily stores bits for ouptput to file or returning to caller
char mask_; // used to access a specific bit in the buffer
std::ostream* os_; // the out-stream object
std::istream* is_; // the in-stream object
};