The problem is when a multi byte variable is stored or transmitted.
Some trasmit the lower significant byte first, other transmit the most significant byte first...So the transmitter and receiver must match on this.
The same is with storing data in a memory.
Some store the least significant byte in the lower address, some store the most significant byte in the lower address:
Example: a 32 bit variable with hex value "0x12345678" is stored in address 0x1000 of an 8 bit system:
either this way:
Address / data
0x1000 / 0x12
0x1001 / 0x34
0x1002 / 0x56
0x1003 / 0x78
or this way:
Address / data
0x1000 / 0x78
0x1001 / 0x56
0x1002 / 0x34
0x1003 / 0x12
The problem is not within one closed sytem where the compiler has the control of all data, The problem arises when two systems access the same data.
For example a profibus controller has dual por RAM inside and and expects a variable stored in RAM with big endian, but the microcontroller stores it little endian in the same RAM area.
He's not asking what is big / little endian, he's asking for a macro to convert endianness.
Here is one. Easy to understand. But imagine that for 64 bits.... it's better to define a swap16,
then a swap32 which uses swap16, then swap64 which uses swap32.
He's not asking what is big / little endian, he's asking for a macro to convert endianness.
Here is one. Easy to understand. But imagine that for 64 bits.... it's better to define a swap16,
then a swap32 which uses swap16, then swap64 which uses swap32.
Basically, there is no almighty endianness converter. If your number has to be byte reversed, you
have to know where it starts and how long it is. Therefore, you may implement a SWAP_ENDIAN16,
SWAP_ENDIAN32 and SWAP_ENDIAN64.
As for the 16 bit version, you can use the exact same method with (x&0x00FF)>>8 + (x&0x00FF) << 8;
Traditionally, Lilliputians broke boiled eggs on the larger end; a few generations ago, an Emperor of Lilliput, the Present Emperor's great-grandfather, had decreed that all eggs be broken on the smaller end after he cut himself breaking the egg on the larger end. The differences between Big-Endians (those who broke their eggs at the larger end) and Little-Endians had given rise to "six rebellions... wherein one Emperor lost his life, and another his crown". The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. The Big-Endians gained favour in Blefuscu.
Traditionally, Lilliputians broke boiled eggs on the larger end; a few generations ago, an Emperor of Lilliput, the Present Emperor's great-grandfather, had decreed that all eggs be broken on the smaller end after he cut himself breaking the egg on the larger end. The differences between Big-Endians (those who broke their eggs at the larger end) and Little-Endians had given rise to "six rebellions... wherein one Emperor lost his life, and another his crown". The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. The Big-Endians gained favour in Blefuscu.