1 / 8

Bit Output using a BitOStream

Bit Output using a BitOStream. next output position. bits already output. data_buffer. 00000 0 10. bit_mask. 00000 1 00. data_buffer holds the bits being output. class BitOStream { private: unsigned char data_buffer ; unsigned char bit_mask ; ofstream fout ;.

soo
Download Presentation

Bit Output using a BitOStream

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bit Output using a BitOStream next output position bits already output data_buffer 00000010 bit_mask 00000100 data_bufferholdsthe bits being output class BitOStream { private: unsigned char data_buffer; unsigned char bit_mask; ofstreamfout; bit_maskspecifiesthe position in the data byte for the next output bit A completely filled data byte will be written to fout The maskcontainsexactly one 1and its position corresponds to the bit positionwhere the next bit thatis output willbewritten.

  2. Bit Input using a BitIStream class BitIStream { private: unsigned char data_buffer; unsigned char bit_mask; unsignedchar nextByte; intbitsInLastByte; boolinLastByte; boolvalidData; boolbitmode; ifstreamfin; data_bufferholds the bits to be input bit_maskspecifiesthe position in the data byte for the next input bit nextByte is a prefetched byte that that will be transferred to the databuffer when its bits are consumed bitsInLastByteis the number of valid bits in the last byte of the file inLastByteis a flag showing if the last byte of the file isbeingprocessed indicates switch from character output to bit output validDataindicates bit is available fin contains the bytes from which bits to be input will be obtained.

  3. //----------------BitOStream.h------------------------------ // Class for bit output on a file.*/ #include <string> #include <fstream> using namespace std; class BitOStream { private: unsigned char data_buffer; unsigned char bit_mask; ofstreamfout; public: BitOStream(string fname); ~BitOStream(); void close(); void putBit(char theBit); void putBitstring(string bits // Following must not be used after any call to putBit or // putBitstring void putChar(unsigned char ch); void putInt(int n); };

  4. //------------------BitOStream.h-----------------------------///------------------BitOStream.h-----------------------------/ // Class for bit input from a file producedwith bit output. / //-----------------------------h-----------------------------/ #pragma once #include <iostream> #include <string> #include <fstream> usingnamespacestd; class BitIStream { private: char data_buffer; char nextByte; char bit_mask; intbitsInLastByte; boolvalidData; boolinLastByte; ifstream fin; boolbitmode;

  5. voidBitOStream::putBit(char theBit) { assert(theBit == '0' || theBit == '1'); if (theBit == '1') data_buffer |= bit_mask; bit_mask <<= 1; if (!bit_mask) { fout << data_buffer; data_buffer = 0; bit_mask = 1; } }

  6. boolBitIStream::getBit(char & theBit) { assert(bitmode); staticintbitsconsumed = 0; if (!validData) { return false; } if (data_buffer & bit_mask) theBit = '1'; else theBit = '0'; bit_mask <<= 1; if (inLastByte) { bitsconsumed++; if (bitsconsumed == bitsInLastByte) validData = false; }

  7. elseif (!bit_mask) // All valid bits in data_buffer have been consumed { bit_mask = 1; if (!inLastByte) { data_buffer = nextByte; fin.get(nextByte); inLastByte = fin.eof(); } else validData = false; } return true; }

  8. symbolCount Leaf symbols . . . . . . . . . Bit encoded code tree Encoded text file symbolCount bytes bits in last byte Output as bytes Output as bits Compressed File Format

More Related