Question
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
Solution
uint32_t reverseBits(uint32_t n) {
uint32_t result = 0;
uint32_t i = 0;
for(i = 1; i != 0; i <<= 1)
{
result <<= 1;
if(n & 1 == 1){
result |= 1;
}
n >>= 1;
}
return result;
}
本文介绍了一种算法,用于将给定的32位无符号整数的二进制位进行逆序操作。例如,输入43261596(二进制为00000010100101000001111010011100),输出964176192(二进制为00111001011110000010100101000000)。
860

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



