#ifndef _BITOPS_H #define _BITOPS_H // // D. L. Frey 11/5/2005 // Based on code written by Steve Tate & Owen Astrachan // Used with permission of the author // // facilitates reading/writing in a "bits-at-a-time" fashion // // Specification: // bits are read/written in the "same" order // i.e., if 15 bits are written, then these bits will be read // in the same order as written if 15 1-bit reads are issued. //----------------------------------------------------------- #include #include using namespace std; //-------------------- // Bit Output Routines //-------------------- // // WriteBits // writes rightmost # bits specified by first parameter // taken from the second parameter to specified stream // (bits may be "buffered" for the next write) // PreConditions: output stream must be opened // numbits > 0 void WriteBits(ofstream& outstream, int numbits, int value); // FlushBits // writes any "left-over" (buffered) bits // to the specified stream // // PreConditions: output stream must be open // Be sure to call this routine after you have written // all the bits and before closing the stream void FlushBits( ofstream& out); //------------------- // Bit Input Routines //------------------- // ReadBits // reads number of bits specified by 2nd parameter from specified // input stream and returns them via ref parameter value // // PreConditions: // Input stream is open // numbits <= 32 // PostConditions: // returns true if numbits were read, otherwise returns false // bool ReadBits(ifstream& input, int numbits, int & value); #endif