uint32_t reverseBits(uint32_t n) {
uint32_t a = 0x80000000;
uint32_t b = 0x1;
while (a>b){
uint32_t atmp = n&a;
uint32_t btmp = n&b;
if (atmp != 0){
n = (n|b);
}
else{
n = (n&~b);
}
if (btmp != 0){
n = (n|a);
}
else{
n = (n&~a);
}
a = a >> 1;
b = b << 1;
}
return n;
}LeetCode: Reverse Bits
最新推荐文章于 2021-05-12 12:33:43 发布
本文介绍了一种使用位操作来反转32位无符号整数的方法。通过不断比较高位和低位,并根据需要进行交换,最终实现整数位的完全反转。

652

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



