我真懷疑我是念資工系的Orz
(大哭)
Reverse Bits
uint32_t reverseBits(uint32_t n) {
int i;
uint32_t new=0,org=0;
for(i=0;i<16;i++)
{
org = (1<<i & n);
new = new | (org | 0) << (31-i*2);
}
for(i=0;i<16;i++)
{
org = (1<<(i+16) & n);
new = new | (org | 0) >> (1+i*2);
}
return new;
}
別人寫的code 好漂釀啊
我寫的為什麼那麼醜呢 XD
欲哭無淚啊嘆氣
[20260208 更新]
為什麼可以這樣寫啊哈哈哈!
為什麼要先把result 往左移一格啊!為什麼不是加完再移呢!
啊~~~~~~(倒地)
int reverseBits(int n) {
int output = 0;
for (int i=0; i<32; i++)
{
output <<= 1;
bool check =n& 0x1;
if (check)
output |= 1;
n= n>>1;
}
return output;
}
沒有留言:
張貼留言