I am working on a project that contains variables of varying different bit sizes from a binary file. For example one line of the file (in hex) may look like "FF C0 AA 00 FE". From this line, for example, the information I need is 4 bits, 7 bits, 11 bits, 8 bits etc. The problem I am having is some of the numbers extracted will be signed and others will be unsigned (4 and 7 bits may be signed, 11 and 8 bits unsigned).
I was originally extracting by masking and shifting the Hex values to obtain a c++ char/short/int of 4, 7, 11, 8 bits. However if I look at the 4 bit in binary it would show up as 00001011. This number should be a negative based off the leading 1 (should only be the 4 bits 1011), but C++ recognizes it as positive since it is looking at all 8 bits.
Another example for clarification, I might extract 11 bits from the file to be (11100101101) but in c++ short format it appears as (0000011100101101), should be signed based of leading 1 in the 11 bits.
I was wondering what an ideal way to handle this would be. I was considering making a bit/byte class, the only problem is with the varying bit sizes (4,7,11,8).
Thanks, hopefully it makes sense. I am fairly new to binary in C++, so there may be a built in function I havent seen.