v9.1.1
Menu

Reverse Bits

easyFREEBit ManipulationBlind 75

Given a 32-bit unsigned integer, reverse its bits and return the resulting integer.

Example

Input
43261596
Output
964176192
Reasoning
  • The input n = 43261596 is a 32-bit unsigned integer, so write it with all 32 positions, leading zeros included: 0000001010010100000111101001110000000010100101000001111010011100. Counting position 00 as the least significant bit, its 1s sit at positions 2,3,4,7,9,10,11,12,18,20,23,252, 3, 4, 7, 9, 10, 11, 12, 18, 20, 23, 25.
  • Reversing the bits sends the bit at position ii to position 31−i31 - i: position 2→292 \to 29, 3→283 \to 28, 25→625 \to 6, and so on. The reversed pattern is 0011100101111000001010010100000000111001011110000010100101000000.
  • Reading that back as a number: 229+228+227+224+222+221+220+219+213+211+28+26=9641761922^{29} + 2^{28} + 2^{27} + 2^{24} + 2^{22} + 2^{21} + 2^{20} + 2^{19} + 2^{13} + 2^{11} + 2^{8} + 2^{6} = 964176192.
  • Notice the leading zeros matter: the input's 6 leading zeros become the output's 6 trailing zeros. Reversing only the 26 significant digits would give a much smaller, wrong number.
  • Another check: 42949672934294967293 is all 1s except position 11, so its single 0 moves to position 3030, giving 232−1−230=32212254712^{32} - 1 - 2^{30} = 3221225471; and 00 stays 00.

Constraints

  • Input is a 32-bit unsigned integer

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.