[LeetCode]190. Reverse Bits
题目描述
思路
类似循环移位的思路
位操作
代码
#include <iostream>
using namespace std;
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 1; i <= 32; ++i) {
uint32_t temp = n << 31;
n >>= 1;
res = res | (temp >> (i - 1));
}
return res;
}
};
int main() {
Solution s;
cout << s.reverseBits(1) << endl;
system("pause");
return 0;
}
本文介绍了如何使用位操作来解决LeetCode上的190题——反转二进制位的问题。通过循环移位的方式,实现了将一个32位无符号整数的所有二进制位进行反转的功能,并给出了具体的C++实现代码。
861

被折叠的 条评论
为什么被折叠?



