LeetCode 190 Reverse Bits

标签: 位操作 LeetCode 发布于:2022-03-04 23:48:59 编辑于:2022-03-05 16:46:20 浏览量:822

概述

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/