I have never had to bother with Big Endian format before. Recently, my work has required me to mess with it a bit and it's a complete headache.
I understand that to convert from little endian (&h12345678) to big endian we just switch the byte order (&h78563412). I get that.
My problem comes when I need to read and write bits. There is a part in a file where each value consists of nine (9) bits. These values are consecutive. This means the first value uses all eight bits of the first byte and the first bit of the second byte. The second value uses the last seven bits of the second byte and the first two bits of the third byte, and so on.
For little endian I would just create a mask, such as &H1FF for nine bits, and mask off the part I need. No matter what I try, I cannot figure out how I would do this for big endian.
So, my question is: How would I read / write a specific amount of bits from a value stored in big endian format?
Thanks.
I understand that to convert from little endian (&h12345678) to big endian we just switch the byte order (&h78563412). I get that.
My problem comes when I need to read and write bits. There is a part in a file where each value consists of nine (9) bits. These values are consecutive. This means the first value uses all eight bits of the first byte and the first bit of the second byte. The second value uses the last seven bits of the second byte and the first two bits of the third byte, and so on.
For little endian I would just create a mask, such as &H1FF for nine bits, and mask off the part I need. No matter what I try, I cannot figure out how I would do this for big endian.
So, my question is: How would I read / write a specific amount of bits from a value stored in big endian format?
Thanks.