views:

284

answers:

2

I would like to read and write n bits from/to binary files. For example, read the next n bits into an integer or the next n bits to a char. I need the data to be bit aligned and not byte aligned.

Are there C++ libraries that allow me to do that?

When I use ostream/istream, I seem to be restricted to using byte aligned data. This is not good enough if I want my data to be packed tightly.

A: 

Byte aligned access is "good enough" if you do the bit-level accessing yourself, of course. :)

Here is a bitstream library that sounds what you want. I haven't tested this myself, but it sounds reasonably minimalistic.

unwind
A: 

You may use below steps to read bits from binary file.

  1. Use mmap to map your binary data file in memory. If your file size is not in multiples of bytes then you will have padding bits at the end of your mapped memory region. Number of padding bits may be anything between 1 to 7.

  2. Now you can read required bits from memory mapped region using bit-masks. Note that you need to use bit-wise AND operaton. The valaue of bit-masks will vary on little-endian and big-endian platform. So, the code will not be portable and needs extra care for portability.

  3. Additionally, you may need to use bitwise right or left rotate operator as well.

You may also type-cast part of mapped memory region as C++ data structures with bit fields. Here, We need to instruct compiler not to use padding in C++ strucutres for byte alignment. This can be achieved using "#PRAGMA PACK(1)" directive. Again, C++ structures with bit fields are not portable across little-endian and big-endian platforms.

The small variation in above method can be used to write bits to binary file.

LionHeart