Leetcode 190 Reverse Bits
#include <stdint.h>
using namespace std;
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
int result = 0;
for (int i = 0; i < 32; i++)
{
int tmp = n & 0x01;//取最后一位
n = n >> 1;//n右移以便取最后一位
/*先或再左移是不对的,最后那次左移破坏了结果
result = result | tmp;//或 最后一位
result = result << 1;//左移*/
result = (result << 1) | tmp;//gcc 和msvc对2^31 2147483468的结果不一样,内部存储不一样?
}
return result;
}
};