In the popular blogpost "The byte order fallacy" from Rob Pike which is often linked in discussions about endianness, it is implied in several places that one should avoid byte swapping, and mentioned that it is a sure sign of trouble. The author goes as far as saying
byte-swapping is the surest indicator the programmer doesn't understand how byte order works.
I will now show an example of byte swapping that I stole from this post:
uint64_t swap64(uint64_t k)
{
return ((k << 56) |
((k & 0x000000000000FF00) << 40) |
((k & 0x0000000000FF0000) << 24) |
((k & 0x00000000FF000000) << 8) |
((k & 0x000000FF00000000) >> 8) |
((k & 0x0000FF0000000000) >> 24)|
((k & 0x00FF000000000000) >> 40)|
(k >> 56)
);
}
I would like you to focus on the claim that byte swapping should be avoided, and I would like you to address any alignment or other issues that could emerge while using a function like swap64()