Search code examples
c++cbitendiannessdisk

Endianness when order of bits (rather than bytes) is crucial


All of the questions and answers on endianness seem to centre on the order of bytes but suppose I've got a series of bits where the order of the bits is crucial. Let's say I save them to a memory stick file as an array of bytes. I may use the memory stick on a little endian machine one day and on a big endian machine the next.

Suppose the bit sequence is as follows

10000000 00000001

I'm on a little endian machine but I always save the bytes as big endian and so I reverse the bits in each byte so they're saved on the memory stick as

00000001 10000000.

Next day I read them into a uint8_t array on a totally different machine. I'm assuming (can someone confirm this) they'll end up in memory as either

{128,1} [little] or {1,128} [big].

So, if I'm on a little endian machine I should again reverse the order of bits within each byte to get back to the original bit sequence. Can anyone confirm?

I'm unsure if the machine reading the bytes (bits) assumes they're in the same endianness of the machine itself.


Solution

  • Endianess only applies to integer (and float) variables of 2 bytes or larger. It does not apply to raw data, uint8_t (byte) variables or arrays of uint8_t.