iq.lab
Python starts when a code cell comes near or you run one
easyBits and math target 15 min

Reverse the 32 bits

Given a whole number n from 0 to 232 - 1, write it in binary as exactly 32 bits, with leading 0s filling the width. Reverse the order of those 32 bits and return the number they make.

Binary is base 2: each digit, called a bit, is 0 or 1 and is worth a power of two, 1, 2, 4, 8 and so on from the right. The leading 0s count: 6 is 110, but as 32 bits it is 29 zeros followed by 110, so its reversal starts with 011 and ends with 29 zeros.

String tools such as format(n, "032b") and int(text, 2) pass the tests. Practice the version that moves bits with &, |, << and >>.

Example 1
Inputn = 6Output1610612736

6 is 29 zeros then 110. Reversed, it is 011 then 29 zeros: 230 + 229 = 1073741824 + 536870912.

Example 2
Inputn = 1Output2147483648

1 is 31 zeros then a 1. Reversed, the 1 comes first: 231.

Example 3
Inputn = 4026531840Output15

4026531840 is 15 × 228: four 1s, then 28 zeros. Reversed, it is 28 zeros, then four 1s, which is 15.

Constraints
  • 0 ≤ n ≤ 232 - 1

  • Treat n as exactly 32 bits, leading 0s included.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.