views:

60

answers:

2

HI, all.

Here are the things I want to do :

I want to store lots of informations to a block by bits, and save it into a file.

In order to keep my file not so big, I only want to use a small number of bits to save specified information instead of a int.

For example, I want to store Day, Hour, Minute to a file.

I only want 5 bit(day) + 5 bit(Hour) + 6 bit(Minute) = 16 bit of memory for data storage.

But I can't find a efficient way to store it into a block to put it into a file.

There are some big problems in my concern :

  1. the data length I want to store each time is not constant. It depends on the income information. So I can't just use a structure to store it.

  2. there must not be any unused bit in my block, I've searched for some topics that mentioned that if I store 30 bits in an int(4 byte variable), then the next 3 bit I save will altomatically go into next int. but I don't want it to happen!!

  3. I know i can use shift right, shift left to put a number to a char, and put the char into a block. but it is way too unefficient.

What I want is to have an char array that I can continue putting specified bits into it, and use write to put it in to a file.

Any resource, web pages, that can help me, please let me know.

I really want to learn how to solve this problem to make my project more perfact.

THANKS!

+1  A: 

I think I'd just use the number of bits necessary to store the largest value you might ever need for any given piece of information. Then, Huffman encode the data as you write it (and obviously Huffman decode it as you read it). Most other approaches are likely to be less efficient, and many are likely to be more complex as well.

Jerry Coffin
A: 

I haven't seen such a library. So I'm afraid you'll have to write one yourself. It won't be difficult, anyway.

And about the efficiency. This kind of operations always need bits shifting and masking, because few CPUs support directly operating into bits, especially between two machine words. The only difference is you or your compiler does the translation.

hpsMouse