Reverse Bits

题目:

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

分析:

每次对2求余并后移一位


代码:

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




阅读更多
文章标签: leetcode
个人分类: leetcode
相关热词: reverse 用reverse
上一篇Find the Difference
下一篇Rotate Array
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭