20
\$\begingroup\$

As most of you probably know, (byte-addressable) hardware memories can be divided into two categories - little-endian and big-endian. In little-endian memories the bytes are numbered starting with 0 at the little (least significant) end and in big-endian ones the other way round.

Fun fact: These terms are based on Jonathan Swift's book Gulliver's Travels where the Lilliputian king ordered his citizens to break their eggs on the little end (thus the little-endians) and the rebels would break theirs on the big end.

How swapping works

Suppose we have an unsigned integer (32bit) 12648430 in memory, in a big-endian machine that might look as follows:

  addr: 0  1  2  3
memory: 00 C0 FF EE

By inverting the byte-order we get the hexadecimal integer 0xEEFFC000 which is 4009738240 in decimal.

Your task

Write a program/function that receives an unsigned 32bit integer in decimal and outputs the resulting integer when swapping the endianness as described above.

Rules

  • Input will always be in the range 0 to 4294967295
  • Output can be printed to STDOUT (trailing newlines/spaces are fine) or returned
  • Input and output are in decimal
  • Behavior on invalid input is left undefined

Test cases

0 -> 0
1 -> 16777216
42 -> 704643072
128 -> 2147483648
12648430 -> 4009738240
16885952 -> 3232235777
704643072 -> 42
3735928559 -> 4022250974
4009738240 -> 12648430
4026531839 -> 4294967279
4294967295 -> 4294967295
\$\endgroup\$
  • \$\begingroup\$ For a function answer, does "input and output are in decimal" mean either a string of digit characters or array of digit values is required? Or can a function answer use its language's natural integer value representation, which in most cases has absolutely nothing to do with "decimal"? \$\endgroup\$ – aschepler Nov 25 '17 at 18:40
  • 1
    \$\begingroup\$ @aschepler The language's integer value, eg. 42 is given in decimal but technically it's in binary in C for example. You can of course type 0x2a, what I wanted to prevent is taking input as a string like "2a" or the like. \$\endgroup\$ – ბიმო Nov 25 '17 at 19:19
  • \$\begingroup\$ Related (as this challenge is just making sure to pad to 32 bits first) \$\endgroup\$ – FlipTack Nov 26 '17 at 13:48

33 Answers 33

1
2
1
\$\begingroup\$

SmileBASIC, 34 bytes

INPUT N
RGBREAD N OUT A,B,C,D?RGB(D,C,A,B)

RGBREAD/RGB are functions that convert between a 32-bit integer and four 8-bit values. They're designed for use with (A)RGB color data but you can use them for other things too.

\$\endgroup\$
1
\$\begingroup\$

C, 50 bytes

#define f(i) (i>>24|i>>8&65280|(i&65280)<<8|i<<24)

Try it online!

To be more portable, I didn't want to use any functions that aren't in ANSI C. :-)

Like @hvd, I also cribbed Polynomial's answer. Due to C's operator precedence, I had to add parentheses around the third term.

\$\endgroup\$
0
\$\begingroup\$

x86 machine code - 11 bytes

     6 00000000 66C1C008                rol ax, 8
     7 00000004 C1C010                  rol eax, 16
     8 00000007 66C1C008                rol ax, 8

Debug it to see value of 0x00C0FFEE has been swapped to 0xeeffc000 in register eax.

% gdb -q swap
Reading symbols from swap...(no debugging symbols found)...done.
(gdb) disassemble _start
Dump of assembler code for function _start:
   0x08048060 <+0>:     mov    eax,0xc0ffee
   0x08048065 <+5>:     rol    ax,0x8
   0x08048069 <+9>:     rol    eax,0x10
   0x0804806c <+12>:    rol    ax,0x8
End of assembler dump.
(gdb) r
Starting program: /home/user/swap 

Program received signal SIGSEGV, Segmentation fault.
0x08048070 in ?? ()
(gdb) i r $eax
eax            0xeeffc000   -285229056
(gdb)
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.