Wednesday 5 October 2016

Reverse Bits (LeetCode)

Problem: Reverse Bits
Reverse bits of a given 32 bits unsigned integer.

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t reverse=0;
        for(int i=0;i<32;i++)
        {
            if(n & (1<<i)) 
                reverse |= (1<<(31-i));
        }
        return reverse;
    }
};

No comments:

Post a Comment