[Leetcode bit operation C++] 190. Reverse Bits

190. Reverse Bits

Insert picture description here

class Solution {
    
    
public:
    uint32_t reverseBits(uint32_t n) {
    
    
        uint32_t temp = 1, bit = 1, ans = 0;
        bit <<= 31;
        while(bit) {
    
    
            if(n & bit) ans += temp;
            temp <<= 1;
            bit >>= 1;
        }
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/m0_37454852/article/details/113935933