KatInterview

190. Reverse Bits

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to reverse the bits of a 32-bit unsigned integer. This means the bit at position i in the input integer should be moved to position 31-i in the output integer. For example, the least significant bit (LSB, at position 0) moves to the most significant bit (MSB, at position 31), and so on.

Solution

Reverse Bits in Python

class Solution:
    def reverseBits(self, n: int) -> int:
        res = 0
        # Iterate through all 32 bits.
        for i in range(32):
            # 1. Left shift the result to make space for the next bit.
            res <<= 1
            
            # 2. Get the least significant bit (LSB) of n.
            lsb = n & 1
            
            # 3. Add this LSB to the result using bitwise OR.
            res |= lsb
            
            # 4. Right shift n to process the next bit in the following iteration.
            n >>= 1
            
        return res

Reverse Bits in Java

class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int result = 0;
        // Iterate 32 times for each bit of the integer.
        for (int i = 0; i < 32; i++) {
            // 1. Left shift the result by 1 to make room for the next bit.
            result <<= 1;

            // 2. Get the least significant bit of n using bitwise AND with 1.
            int lsb = n & 1;
            
            // 3. Add (bitwise OR) this LSB to our result.
            result |= lsb;

            // 4. Right shift n to discard the LSB and prepare for the next bit.
            // Note: using >> (arithmetic shift) is fine here because we only care about
            // the LSB extracted by '& 1', so sign extension doesn't affect the result.
            n >>= 1;
        }
        return result;
    }
}

Reverse Bits in C++

class Solution {
public:
    int reverseBits(int n) {
        int result = 0;
        // Iterate through all 32 bits of the integer.
        for (int i = 0; i < 32; ++i) {
            // 1. Left shift the result to make space for the next bit.
            result <<= 1;
            
            // 2. Extract the least significant bit (LSB) of n.
            int lsb = n & 1;
            
            // 3. Add this LSB to the result.
            result |= lsb;
            
            // 4. Right shift n to process the next bit in the next iteration.
            n >>= 1;
        }
        return result;
    }
};

Reverse Bits in TypeScript

function reverseBits(n: number): number {
    let result = 0;
    // Iterate 32 times for each bit of the integer.
    for (let i = 0; i < 32; i++) {
        // 1. Left shift the result by 1 to make room for the next bit.
        result <<= 1;

        // 2. Get the least significant bit (LSB) of n.
        const lsb = n & 1;

        // 3. Add this LSB to our result.
        result |= lsb;

        // 4. Right shift n to discard the LSB and process the next bit.
        n >>= 1;
    }

    // In TypeScript/JavaScript, bitwise operations treat numbers as 32-bit signed integers.
    // The result might be interpreted as negative if its MSB is 1.
    // The unsigned right shift `>>> 0` converts the signed 32-bit integer 
    // to its unsigned representation, returning a non-negative number.
    return result >>> 0;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.