190. Reverse Bits

Reverse Bits (LeetCode 190) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Bit Manipulation Patterns pattern, in the Bitwise AND - Counting Set Bits (Hamming Weight) subpattern.

How to solve Reverse Bits

Reverse Bits is a Hamming Weight problem, part of Bit Manipulation Patterns. Count number of 1 bits (Hamming weight) using bitwise AND operations.

Work towards the solution in these steps:

  1. The operation always processes exactly 32 positions, even when the numerical value appears to have fewer significant bits.
  2. Build a separate result while examining `n` one bit at a time.
  3. A right shift can expose the next input bit, while a left shift makes room in the result.

The reference solution runs in O(1) time and O(1) space.

The Reverse Bits solution and editorial walks through the full approach with reference code.

Problem statement

n is a signed 32-bit integer, but its bits are treated as a fixed-width 32-bit two's-complement pattern. A two's-complement pattern uses bit 31 as the sign bit and bits 30 through 0 for the remaining value representation; leading zero bits are part of the pattern.

Reverse the order of all 32 bits in n: the original bit at position 0 becomes position 31, the original bit at position 1 becomes position 30, and so on. Interpret the resulting 32-bit pattern as a signed two's-complement integer.

Implement Solution.reverseBits(n) and return that signed integer. Both n and the returned value are signed 32-bit integers.

Example 1:

Input: n = 1 Output: -2147483648 Explanation: The 32-bit pattern for 1 has only its least significant bit set. After reversal, that set bit occupies position 31, which represents -2^31 as a signed 32-bit integer.

Example 2:

Input: n = 5 Output: -1610612736 Explanation: The set bits of 5 are at positions 0 and 2. They move to positions 31 and 29, producing the signed 32-bit value -1610612736.

Example 3:

Input: n = -2 Output: 2147483647 Explanation: The 32-bit pattern for -2 is all ones except for its least significant bit. Reversing it gives a pattern with bit 31 clear and every lower bit set, equal to 2147483647.

Constraints:

- -2^31 <= n <= 2^31 - 1 - n is an integer - n is represented using exactly 32 bits, including leading zero bits - The returned value is interpreted as a signed 32-bit two's-complement integer - -2^31 <= returned value <= 2^31 - 1

Topics and companies

Topics: Bit Manipulation, Iteration, Math.

How to practise Reverse Bits on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(1) time and O(1) space.

Open Reverse Bits in the code editor, or read the Reverse Bits editorial for a worked solution with its approach and complexity analysis.

Where Reverse Bits sits in the DSA pattern sheet

Problems related to Reverse Bits

Other problems that use the same Bitwise AND - Counting Set Bits (Hamming Weight) and Bit Manipulation Patterns techniques:

Reverse Bits FAQ

What is the LeetCode number of Reverse Bits?

Reverse Bits is LeetCode problem 190, rated Easy.

What pattern is used to solve Reverse Bits?

Reverse Bits is a Hamming Weight problem. It belongs to the Bit Manipulation Patterns pattern, in the Bitwise AND - Counting Set Bits (Hamming Weight) subpattern.

What is the time complexity of Reverse Bits?

The reference solution runs in O(1) time and O(1) space.

Preparing this workspace.