• ### Announcements

#### Archived

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

# Writing bits to a file

## Recommended Posts

Bleakcabal    122
Hi, I am writing an Huffman compression class. Up to now I have succesfully implemented a method that build a frequency table, and another one that build the hufmann tree. Now all I need to do is compress/decompress the file. For that I am left with bits representing each character. Exemple, t could be 10101 and a 111 and c 10. So how do I write these directly to a virgin file ? I know how to open/create a file in binary mode : fopen(file_pointer, "w+b"); But when I write something to if I write 101010 it will convert this to many many bits representing this string of number. There must be a way to do this ? BTW im using C++ with nothing else. WHO DO THEY THINK THEY''RE FOOLING : YOU ?
GARAL website

#### Share this post

##### Share on other sites
You will need to stick all the bits together and write out as a series of bytes. It''s probably best to fill a buffer with the individual bits, then dump this to the file when it gets to a certain number of bytes in size.

#### Share this post

##### Share on other sites
Bytes are the smallest data type you can work with directly. So, to implement Anonypous Moster''s idea, you''d do this:

  void Write_Bit ( int BitIndex, bool Value ){ int ByteIndex = BitIndex >> 3; // BitIndex / 8 int BitOffset = BitIndex & 7; // BitIndex % 8 bool OldValue = ByteArray[ByteIndex] & (1 << BitOffset); if( Value && !OldValue ) ByteArray[ByteIndex] += 1 << BitOffset; else if( !Value && OldValue ) ByteArray[ByteIndex] -= 1 << BitOffset;}

~CGameProgrammer( );

#### Share this post

##### Share on other sites
Bleakcabal    122
Is ByteArray a typo ? I don''t see it declared nowhere ? What type is it ? bool, int ? And the parameter bool Value seems like an unrefenced local variable.

Could someone clarify this ? What is the BitIndex parameter suppose to hold precisly if bool Value contains some sort of bits number ?

WHO DO THEY
THINK THEY''RE
FOOLING : YOU ?

GARAL website

#### Share this post

##### Share on other sites
Nekosion    122
From what I know of huffman...

You technically cannot get
t = 10101
a = 111
c = 10

The ending bit must always be either 0 or 1.

Otherwise I have this mixed up with some other form of compression.

#### Share this post

##### Share on other sites
Nekosion    122
From what I know of huffman...

You technically cannot get
t = 10101
a = 111
c = 10

The ending bit must always be either 0 or 1.

Otherwise I have this mixed up with some other form of compression.

#### Share this post

##### Share on other sites
Bleakcabal    122
I didnt notice maybe, I followed an article detailing how the algorythm works ( found on gamedev ) that didnt include any code. The value I submitted before is bogus and was just used to illustrate my point.

WHO DO THEY
THINK THEY''RE
FOOLING : YOU ?

GARAL website

#### Share this post

##### Share on other sites
DmGoober    136
Didn't read the other peoples code very carefully, but you can do something like this (assume that the file is a textfile, not a binary file): (Their code is extremely well written, but because it is so concise it is hard to follow).

int numbits = 16;
char bits[numbits];
bits[0] = 1; bits[1] = 0; . . . //etc. for all bits.
int bitcounter=0;
int onbit = 0;
char bitmask = 128; //this is used cause binary is 10000000
char output;
for (bitcounter = 0; bitcounter < numbits; bitcounter++)
{
if (onbit == 8) //eight bits to character or byte
{
onbit = 0;
bitmask = 128;
output = 0;
fputc (file, output);
}
if (bits[bitcounter])
{
output = output | bitmask;
}
onbit++;
bitmask = bitmask >> 1;
}
fputc (file, output);

Warning: this code will put garbage bits at the end of the file. Make sure to specify how long the file is at the beginning.

Basically what this file is doing is keeping a character, output that looks like this:
00000000

Then it goes through the array of bits. If a bit is one than it ORs the output with the mask:
00000000 OR'd with
10000000
----------
10000000

The mask is shifted left every bit that we encounter, so on the fourth bit, for example it will look like this:
10000000 old ouput
00010000 bitmask
----------
10010000 newoutput

Hope that helps.

DmGoober

Edited by - dmgoober on January 30, 2001 2:31:03 AM