Reverse Bits

 1 class Solution {
 2 public:
 3     uint32_t reverseBits(uint32_t n) {
 4         uint32_t ans=0;
 5         int t=0;
 6         while(n)
 7         {
 8             t++;
 9             ans<<=1;
10             if(n&0x1)
11             {
12                 ans++;
13             }
14             n>>=1;
15         }
16         ans<<=(32-t);
17         return ans;
18     }
19 };
View Code

注意普通的翻转只是将数翻转,1翻转之后还是1,现在的翻转是32位翻转

也可以通过for(i=1;i!=0;i<<=1)来控制结束的位数

原文地址:https://www.cnblogs.com/varcom/p/4565861.html