#### Archived

This topic is now archived and is closed to further replies.

# Bit Streams

This topic is 5786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anybody know how to write a bit stream in C/C++? :o)

Yes.

##### Share on other sites
I don''t suppose you could point me towards some resources on the topic?

##### Share on other sites
quote:
Original post by Pellanor
I don't suppose you could point me towards some resources on the topic?

I can.

Here.

[edited by - fallenang3l on April 18, 2002 3:42:35 PM]

##### Share on other sites
Is there a reason you guys are being bastards?

If I had found anything decent on a search engine, would I be asking here?

so please, can somebody please explaing to me how to write a bit stream in C++? Or at least give me a link to a sight that has a good explination.

##### Share on other sites
Because it''s an extremely general question, so any comprehensive post that covers all cases would span several pages and isn''t worth our time. If you haven''t started attacking the problem (because we see no evidence that you have, e.g. platform, library, code), and when you get stuck, ask a specific question and you''ll get a specific answer.

##### Share on other sites
Okay, sorry about that.

Here is my situation.
I''m attempting to write a compression algorithim(sp) based on Huffman Encoding. so far my partner has the tree going okay. I''m in charge of writing the data to the file, bit by bit, and I haven''t a clue how. Any help or suggestions on the topic would be greatly appriciated. Once again sorry about my little rant above.

##### Share on other sites
I just wrote a huffman encoding project for my CS 223 class. If you would like my visual studio project email me at needlessmail@go.com I will check this email and then reply with the project attached.

##### Share on other sites
You need to write a routine that will do basically serial-to-parallel conversion. Do you get all the bits at once out of an array, or are you getting them one at a time? If you get them all at once, you can write a function that forms a byte out of 8 bits in an array. If you have to get the bits one at a time, I''d write a class that gets an open file stream, takes in a bit at a time, and every time it has 8 bits it writes that character to the file. It''d need a flush function for the last bits, and depending on how you do shifting, you have to make sure the trailing byte is padded correctly.

The only technical stuff you need to know how to use is the bitshift operator (direction depends on how you want to do it) and bitwise or.

Hope that helps. If you need more help, post again, but beware that we rarely give direct answers to homework problems--we will give hints, though.

##### Share on other sites
This si an implementation i did once:

  // unwritten stream area must be prezeroedvoid ClassHuffman::EmitCode(unsigned long dwCode, 							char nCodeLen, 							unsigned char *pStream, 							unsigned long *dwBitPtr){	unsigned long dwBytePtr;	unsigned short wInBytePtr;	char i;	unsigned char chMask;	dwBytePtr = *dwBitPtr >> 3;	wInBytePtr = (unsigned short)(*dwBitPtr % 8);	for(i=0; i<nCodeLen; i++){		chMask = (unsigned short)(dwCode >> i) & 0x01;		if(chMask){			pStream[dwBytePtr] |= (chMask<<wInBytePtr);		}		(*dwBitPtr)++;		dwBytePtr = *dwBitPtr >> 3;		wInBytePtr = (unsigned short)(*dwBitPtr % 8);	}}

Forever trusting who we are
And nothing else matters
- Metallica