Posted on

Description

Submission

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        if(n == 0) return 0;
        uint32_t one = 1;
        uint32_t res = 0;
        int count = 0;
        for(; n != 0; n >>= 1, count++) {
            res <<= 1;
            res += (one & n);
        }
        res <<= (32 - count);
        return res;
    }
};

Submission 210329

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

Leave a Reply

Your email address will not be published. Required fields are marked *