写一个函数逆转整数的二进制表示 unsigned ReverseBit(unsigned x);
unsigned x = RevBit(0xf0ec9999);
x应该为 0x9999370f。
0xf0ec9999 == 11110000111011001001100110011001(二进制)
unsigned x = RevBit(0xf0ec9999);
x应该为 0x9999370f。
0xf0ec9999 == 11110000111011001001100110011001(二进制)
0x9999370f == 10011001100110010011011100001111(二进制)
解法一直接做:
#define UNSIGNED_BITS_COUNT 32
unsigned int ReverseBit(unsigned int input)
{
unsigned int ret, i;
for(ret = i = 0; i < UNSIGNED_BITS_COUNT; i++, input = input >> 1)
ret = (ret << 1) | (input & 1);
return ret;
}
解法二:
思路:相邻两位互调位置(即一位换一位),再