#include "bit_utils.hh" int get_bit(istream& myin) { /* return the next bit of the input, -1 if EOF */ static char bits = 0; static int count = 8; /* number of bits already sent from "bits" */ if (count == 8) { if (!myin.get(bits)) return -1; count = 0; } return ((bits >> (count++)) & 1); } static int p_bits = 0; static int p_count = 0; /* number of bits currently in p_bits */ void put_bit(ostream& myout, int b) { /* puts bit b into the output */ p_bits |= (b << p_count); p_count++; if (p_count == 8) { myout.put(p_bits); p_count = 0; p_bits = 0; } } void put_finish(ostream& myout) { /* puts out the remaining byte */ if (p_count != 0) myout.put(p_bits); }