Skip to main content
GameDev.net gamedev.net
🔒 Locked

Bit Streams

Started by Pellanor Apr 18, 2002 at 11:40 AM 8 replies 350+ views
Original Post
Pellanor
Pellanor
Does anybody know how to write a bit stream in C/C++? :o)
Stoffel
Stoffel
Yes.
Pellanor
Pellanor
I don''t suppose you could point me towards some resources on the topic?
fallenang3l
fallenang3l
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]
Pellanor
Pellanor
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.
Stoffel
Stoffel
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.
Pellanor
Pellanor
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.
GAG34
GAG34
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.
Stoffel
Stoffel
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.
Gabriel Fleseriu
Gabriel Fleseriu
This si an implementation i did once:

  
// unwritten stream area must be prezeroed

void 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
Forever trusting who we areAnd nothing else matters - Metallica

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.