4

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.

NoComprende
  • 731
  • 4
  • 14
  • Statements about endianness centre on the order of bytes because typically endianness only refers to the ordering of bytes. I may be mistaken, but I don't think the order of bits in a byte changes from little to big-endian machines. – jtb Jul 09 '20 at 13:41
  • 2
    Why do you reverse the bits in the first place? Bits "just exist" within bytes, the most-significant bit remains in the same place. – Thomas Jager Jul 09 '20 at 13:41
  • Endianness only comes into play when you read and write integers which occupy more than one byte. – August Karlstrom Jul 09 '20 at 13:42
  • 2
    Bits only have endian-ness when streamed over a serial pipe, like a modem. – Eljay Jul 09 '20 at 13:50
  • https://linux.die.net/man/3/byteorder – jo_ Jul 09 '20 at 13:52
  • 1
    IBM mainframes numbered bits from 0 (msb) to 31 (lsb), however it's just their convention. Transferring bytes to a mainframe did not swap the bit order, just the name of the bits changed. – stark Jul 09 '20 at 13:57
  • The duplicate question is edited with LAST EDIT: I found this (https://www.linuxjournal.com/article/6788) which says "Bit order usually follows the same endianness as the byte order for a given computer system". Does that not back up what I'm saying? I'm in the process of reading that link but it's not easy to digest. As someone commenting on the duplicate said - all of the answers are bad. – NoComprende Jul 09 '20 at 14:30
  • Don't swap the bit order! You will find that it Just Works. You only need to be concerned with bit order if you are dealing with low-level communication or data bus contents. – TonyK Jul 09 '20 at 15:39
  • @NoComprende "all of the answers are bad." is incorrect. [Answer](https://stackoverflow.com/a/25402731/2410359) is close enough to being correct. In your case, rather than describe code with "I always save the bytes as big endian", post the code used. Then one can fully answer the impact of bit-endianness in your case - else this is too abstract. – chux - Reinstate Monica Jul 09 '20 at 15:46
  • https://www.embeddedrelated.com/showthread/comp.arch.embedded/60464-1.php was 10 pages of the same but mostly backs up what you are all saying regarding endianness not applying to bits. Thanks for the replies. – NoComprende Jul 09 '20 at 16:05
  • @stark Why the hide & seek event at IBM wasn't a great success: count to 100 then turn around. One second later: "100! Ready or not, here I come!" The only participants that escaped were those who quickly managed to hide inside gaps in the middle of a EBCDIC table. – Lundin Jul 10 '20 at 09:19

1 Answers1

1

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    Bit endianness can show itself in bit fields member's order when overlaying with a `union`. Typically though, bit endianness is not important in C code, but perhaps in serial communication. – chux - Reinstate Monica Jul 09 '20 at 15:31
  • @ chux - Reinstate Monica Unfortunately for me it's bit fields I'm particularly interested in. I wrote an sqlite function to pack fields into a blob using as few bits as possible. It worked a treat on my little endian laptop but I've been unable to find the info I need to make it work on a big endian machine. – NoComprende Jul 10 '20 at 08:58