
Leetcode Problem 190: Reverse Bits
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:
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))