22
\$\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\$
3
  • \$\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
    Commented Nov 25, 2017 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\$ Commented Nov 25, 2017 at 19:19
  • \$\begingroup\$ Related (as this challenge is just making sure to pad to 32 bits first) \$\endgroup\$
    – FlipTack
    Commented Nov 26, 2017 at 13:48

35 Answers 35

1
2
1
\$\begingroup\$

K4, 18 bytes

Solution:

0b/:,/8#|12 8#0b\:

Examples:

q)\
  0b/:,/8#|12 8#0b\:0
0
  0b/:,/8#|12 8#0b\:1
16777216
  0b/:,/8#|12 8#0b\:42
704643072
  0b/:,/8#|12 8#0b\:4294967295
4294967295
  0b/:,/8#|12 8#0b\:4026531839
4294967279

Explanation:

There are no unsigned ints, so takes input as a long.

Convert into boolean array (64bits), reshape, reverse, take first 8 bytes, convert back to long.

0b/:,/8#|12 8#0b\: / the solution
              0b\: / convert to bits
         12 8#     / reshape into 12x8 grid (wraps)
        |          / reverse
      8#           / take first 8
    ,/             / flatten
0b/:               / convert to long

Bonus:

19 byte version in oK which you can Try online!

2/,/8#|12 8#(64#2)\
\$\endgroup\$
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\$

Thunno 2, 9 bytes

ɠ⁴+ɠBḣrɠḋ

Try it online!

Explanation

ɠ⁴+ɠBḣrɠḋ  # Implicit input
ɠ⁴         # Push 256 ** 4
  +        # Add to the input
   ɠB      # Convert to base 256
     ḣ     # Remove the leading 1
      r    # Reverse the list
       ɠḋ  # Convert from base 256
           # Implicit output
\$\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\$
0
\$\begingroup\$

Desmos, 51 48 bytes

-3 bytes thanks to @Aiden Chow!

f(n)=∑_{i=0}^3mod(floor(n/256^i),256)256^{3-i}

Try it on Desmos!

Extracts each byte and adds them back in reverse order.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Just some very trivial syntax golfing can save 3 bytes: f(n)=∑_{i=0}^3mod(floor(n/256^i),256)256^{3-i} \$\endgroup\$
    – Aiden Chow
    Commented Aug 19, 2023 at 6:11
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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