UVa 594

From Algorithmist
Jump to navigation Jump to search

594 - One Little, Two Little, Three Little Endians[edit]

Summary[edit]

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[edit]

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[edit]

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

Notes[edit]

  • An understand of signed/unsigned bitwise operators helps greatly (NOT, AND, OR, SHIFTS, etc...)
  • Be careful using memcpy!!!

Implementations[edit]

  • Use bitwise operators.
  • Memcopy

Optimizations[edit]

None.

Input[edit]

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

Output[edit]

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

Solutions[edit]

References[edit]

  1. http://en.wikipedia.org/wiki/Endianness