I am trying to read bits from a buffer of bytes. So far masking and shifting has worked however I am not sure what to do about a value that appear between bytes, such as,
... 11001001 11101101 ... (read 8 bits from the buffer with 4 bit offset)
|_______|
8 and 4 are not static, can differ, for example:
... 11001001 11101101 ... (read 7 bits from the buffer with 6 bit offset)
^ |______|
|
Buffer
I have a starting offset, and a total of bits to read. However I have a char buffer, and I can't bit shift two bytes into one by using chars (Or maybe I can ?), so I thought of doing this:
uint16_t two_byte_buffer;
memcpy(&two_byte_buffer, real_buffer, 2); // May need to endian convert ?
And now I can bit-shift the 16-bit integer value to read the necessary 8 bits. Would this method work ? And is there a better method ?
Edit: So far I have used this method to extract bits from a single char.