594 - One Little, Two Little, Three Little Endians
Different computer hardware manufactures store data differently, one of these differences Endianness. Endianness refers to the order of the bytes in memory, that is whether the highest significant byte is stored higher or lower than the less significant bytes. Big Endian stores the most significant byte first (lowest memory address), while Little Endian stores the lest significant byte first. This program will convert Little Endian to Big Endian and Big Endian to Little Endian.
One solution is to extract each byte of the solution using masks, shift each byte appropriately, and finally using a bitwise OR to merge the bytes back to one. Essentially just reverse the bytes of a 32-bit signed integer.
11111111 00000000 10101010 01010101
01010101 10101010 00000000 11111111
- Make sure you reverse it 8-bits at a time
- An understand of signed/unsigned bitwise operators helps greatly (NOT, AND, OR, SHIFTS, etc...)
- Be careful using memcpy!!!
- Use bitwise operators.
0 123456789 -123456789 1 16777216 20034556 -1 -2 -65536
0 converts to 0 123456789 converts to 365779719 -123456789 converts to -349002504 1 converts to 16777216 16777216 converts to 1 20034556 converts to -55365375 -1 converts to -1 -2 converts to -16777217 -65536 converts to 65535