LeetCode 190 Reverse Bits

Tag: 位操作 LeetCode Posted on 2022-03-04 23:48:59 Edited on 2022-03-05 16:46:20 Views: 156

概述

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;
    }
};

未经允许,禁止转载,本文源站链接:https://iamazing.cn/