Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
例如,给定输入为43261596(二进制表示为00000010100101000001111010011100),应该返回964176192(二进制表示为00111001011110000010100101000000)
思考:能不能降低函数的调用次数?
然而我用的最蠢的办法 —— 一个个读←_←话说今天状态不太好,而且明天还有操作系统实验,所以大概会少做几道,之后会补上的。
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t temp=0;
int i=0;
for(;i<32;i++)
{
temp*=2;
temp+=((n>>i)&0x01);
}
return temp;
}
};