方法一 顺序反转
uint32_t reverseBits(uint32_t n) {
uint32_t i =0;
uint32_t temp =0;
for(i = 0; i < 32; i++){
temp <<= 1;
if(n & 0x01){
temp |= 0x01;
}
n >>= 1;
}
return temp;
}
方法二:按字节逆序
unsigned char fun(unsigned char c)
{
c = (c & 0xaa) >> 1 | (c & 0x55) << 1;
c = (c & 0xcc) >> 2 | (c & 0x33) << 2;
c = (c & 0xf0) >> 4 | (c & 0x0f) << 4;
return c;
}
uint32_t reverseBits(uint32_t n) {
uint32_t temp = 0;
for (int i = 0; i < 4; i++)
{
temp <<= 8;
temp |= fun(n & 0xff);
n >>= 8;
}
return temp;
}