22
\$\begingroup\$

Write the shortest code to reverse the bit order of a 32-bit integer.

Rules:

  1. Input is assumed to be a valid integer or string equivalent if your language doesn't support numerical values (e.g. Windows Batch).
  2. Output must be a valid integer or string equivalent if your language doesn't support numerical values (e.g. Windows Batch).
  3. Standard library only.
  4. It may be a function or a complete program.
  5. Input may be either from stdin or as a function argument.
  6. Output must be either stdout or as a returned value.
  7. If your language has a built-in or standard library function that does this in one step (e.g. rbit in ARM assembly), that cannot be used.

Examples:

Key:

  1. decimal
    • binary
    • (reverse)
    • reversed binary
    • decimal output

Examples:

  1. -90 (8-bit example for demonstration)

    • 10100110b
    • (reverse)
    • 01100101b
    • 101
  2. 486

    • 00000000000000000000000111100110b
    • (reverse)
    • 01100111100000000000000000000000b
    • 1736441856
  3. -984802906

    • 11000101010011010001100110100110b
    • (reverse)
    • 01100101100110001011001010100011b
    • 1704506019

Note: Omissions are free game. If I didn't say it, and it's not one of the standard loopholes, then it's completely allowed.

\$\endgroup\$
10
  • \$\begingroup\$ What is meant by "omissions" in "omissions are free game"? \$\endgroup\$ Aug 15, 2014 at 18:01
  • 1
    \$\begingroup\$ Anything not explicitly stated in the rules. \$\endgroup\$
    – Claudia
    Aug 15, 2014 at 18:05
  • \$\begingroup\$ Would a 16gb static table be counted as part of the program length? \$\endgroup\$
    – Hot Licks
    Aug 15, 2014 at 21:24
  • \$\begingroup\$ @HotLicks According to the typical interpretation of program, yes. \$\endgroup\$
    – FUZxxl
    Aug 15, 2014 at 22:08
  • \$\begingroup\$ language that only supports 8-bit inputs, can we take input as four 8-bit numbers? \$\endgroup\$
    – Sparr
    Aug 16, 2014 at 18:57

36 Answers 36

1
2
0
\$\begingroup\$

Perl (37+1)

basically a port of the C solution by Todd Lehman

perl -E '$t=<>;map$r=2*$r|1&$t>>$_,0..31;say$r'

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

JavaScript (ES6), 32 bytes

(n,m=32)=>(--m&&f(n,m)*2)|n>>m&1

Wrote this while trying to solve Bit-Reversal Permutations - it didn't help there, but it works fine here!

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

PASM (P8X32A assembly language), 4 bytes, 1 instruction

REV $1ef, #0

Assembles to:

00 DE FF 3C

Explanation:

Reverses the low 32-0 bits (e.g. all of them) in place in location 0x1ef.

Quite why this instruction exists is a mystery to me. Probably to make life extremely awkward for emulator authors. :)

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

C, 40 38 bytes

using a recursive function and multiplication by 1u to cast from int to unsigned

R(I){return I?I<<31|R(I*1u/2)*1u/2:0;}

edit: replaced right shift with division by 2

Try it online!

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

05AB1E, 9 bytes

b32jð0:RC

Try it online!

Uses unsigned integers.

b   convert input to binary
32j pad to length 32
ð0: replace spaces by zeroes
R   reverse
C   convert to decimal

I don't know if that counts as "omissions", but if input and output are allowed in binary, then the one-byter R will do the work.

\$\endgroup\$
3
  • \$\begingroup\$ First of all, OP explicitly stated that I/O must be in decimal, and even if this wasn't the case R still breaks the rule that built-ins aren't allowed. \$\endgroup\$
    – Makonede
    Apr 1, 2021 at 16:58
  • \$\begingroup\$ Ok, then the single "R" isn't allowed, but the 9-byte answer is. \$\endgroup\$
    – Dorian
    Apr 19, 2021 at 8:16
  • \$\begingroup\$ Yes, that is correct. \$\endgroup\$
    – Makonede
    Apr 19, 2021 at 15:51
0
\$\begingroup\$

Factor, 33 bytes

[ 32 <bits> reverse bits>number ]

Try it online!

Explanation:

  • 32 <bits> represent an integer as a virtual sequence of 32 bits (booleans)
  • reverse reverse the sequence
  • bits>number convert a sequence of booleans to an integer
\$\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.