LeetCode 190 Reverse Bits
解法1:
1 class Solution{ 2 public: 3 uint32_t reverseBits(uint32_t x){ 4 uint32_t res=0; 5 for (int i=0;i<32;++i) { 6 res |= ((x>>i)&1)<<(31-i); 7 } 8 return res; 9 } 10 };
解法2:
class Solution{ public: uint32_t reverseBits(uint32_t x){ uint32_t res=0; for (int i=0;i<32;++i) { if ((x&1)==1) { res = res<<1; res = res+1; x = x>>1; } else{ res = res<<1; x = x>>1; } } return res; } };