# Reverse Bit Order of 32-bit Integers

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.

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

# 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'

## 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!

# 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. :)

# 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!

# 05AB1E, 9 bytes

b32jð0:RC


Try it online!

Uses unsigned integers.

b   convert input to binary
ð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.

• 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. Apr 1, 2021 at 16:58
• Ok, then the single "R" isn't allowed, but the 9-byte answer is. Apr 19, 2021 at 8:16
• Yes, that is correct. Apr 19, 2021 at 15:51

# 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