leetcode 190:颠倒二进制位

题目:

算法思想:颠倒相当于把二进制数看成一个环,朝一个方向移动32次就行了。

代码:

    uint32_t reverseBits(uint32_t n) {
        uint32_t result = 0;
        for(int i = 0;i < 32;i++)
        {
            result = result << 1;
            result += n & 1;
            n = n >> 1;
        }
        return result;
    }
发布了137 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wh_computers/article/details/99622114