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;
}
解法二:
思路:相邻两位互调位置(即一位换一位),再相邻的两位换两位,在相邻的四位与四位互调位置,再八位与八位互调位置,最后前十六位和后十六位互换位置,完成32位整数逆转。
412




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



