> Uploading knowledge... _
[░░░░░░░░░░░░░░░░░░░░░░░░] 0%
blog logo
> CHICIO CODING_Pixels. Code. Unplugged.

Reverse Bits

Leetcode Problem 190: Reverse Bits

Problem Summary

Reverse the bits of a given 32-bit unsigned integer and return the resulting integer. For example:

Integer     | Binary (32 bits)
------------|----------------------------------
43261596    | 00000010100101000001111010011100
964176192   | 00111001011110000010100101000000

Reversing bits of 43261596 produces 964176192.

Follow up: If this function is called many times, how would you optimize it?

Constraints:

  • The input is a non-negative even integer in the range [0, 2^31 − 2].

Techniques

  • Divide and Conquer
  • Bit Manipulation

Solution

function reverseBits(n: number): number {
    let reversedN = 0

	for (let i = 0; i < 32; i++) {
        let currentBit = n & 1
        reversedN = (reversedN << 1) | currentBit;
        n = n >>> 1 
    }

    return reversedN >>> 0
};

console.log(reverseBits(43261596))