[leetcode]190. Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

Example:

Input: 43261596
Output: 964176192
Explanation: 43261596 represented in binary as 00000010100101000001111010011100, 
             return 964176192 represented in binary as 00111001011110000010100101000000.

分析:

返回将一个无符号整数的32位二进制颠倒之后的十进制数。把要翻转的数从右向左一位位的取出来,如果取出来的是1,我们将结果res左移一位并且加上1,然后将n右移一位;如果取出来的是0,我们将结果res左移一位,再将n右移一位即可。

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

猜你喜欢

转载自blog.csdn.net/weixin_41814716/article/details/84847433