LeetCode 190 Reverse Bits

逆转所有位

标签:位操作LeetCode发布于:编辑于:浏览量:1392

概述

https://leetcode.com/problems/reverse-bits/

直接法

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t a = 0;
        for (int i = 0; i < 32; i ++) {
            auto t = n >> i;
            if (t % 2 == 1) {
                int num = 31 - i;
                auto mask = 1 << num;
                a = a | mask;
            }
        }
        return a;
    }
};