You may use below steps to read bits from binary file.
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.
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.
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.