UVa 594 - One Little, Two Little, Three Little Endians

Summary
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.

Explanation
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 becomes 01010101 10101010 00000000 11111111

Gotchas

 * Make sure you reverse it 8-bits at a time

Implementations

 * Use bitwise operators.
 * Memcopy

Optimizations
None.

Input
0 123456789 -123456789 1 16777216 20034556 -1 -2 -65536

Output
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