码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode. 颠倒二进制位

时间:2019-10-16 23:03:47      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:for   return   block   分析   代码   pre   赋值   rev   题目   

题目要求:

颠倒给定的 32 位无符号整数的二进制位。

示例:

输入: 00000010100101000001111010011100
输出: 00111001011110000010100101000000
解释: 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596,
因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。

代码:

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

分析:

会对二进制数末尾进行赋值

LeetCode. 颠倒二进制位

标签:for   return   block   分析   代码   pre   赋值   rev   题目   

原文地址:https://www.cnblogs.com/leyang2019/p/11688987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!