34 · Bit Manipulation

#190 Reverse Bits

Easysolve on LeetCode ↗

frequently reported atNVIDIAAirbnbAppleAmazonMicrosoft

the problem

In plain words

Reverse the bits of a given 32-bit signed integer: bit 0 becomes bit 31, bit 1 becomes bit 30, and so on. In Java the input and output are plain ints holding the 32-bit pattern.

in n = 43261596 (00000010100101000001111010011100)

out 964176192 (00111001011110000010100101000000)

in n = 2147483644 (01111111111111111111111111111100)

out 1073741822 (00111111111111111111111111111110)

  • 0 ≤ n ≤ 2³¹ − 2 and n is even (current version)
  • Older versions passed any 32-bit unsigned pattern; the code below handles negative ints too

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #191
      Number of 1 Bits
      Easyin Bits
    2. #7
      Reverse Integer
      Mediumin Math & Numbers
    3. #231
      Power of Two
      Easyin Bits