链接: https://leetcode.com/problems/reverse-bits/
此题的关键是预先将1<<i的数字存入一个大小为32的数组中,然后通过x & (1 << i)来获得x的第i位是不为0的判断.进行求和即可。
class Solution { public: Solution(){ unsigned int i = 0; unsigned int j = 1; for(; i < 32; i++) a[i] = (j<<(31-i)); } unsigned int reverseBits(unsigned int n) { unsigned int reverseBit = 0; for(unsigned int i = 0; i < 32; i++){ if((n & (1 << i)) != 0) reverseBit += a[i]; } return reverseBit; } private: unsigned int a[32]; };
原文地址:http://blog.csdn.net/lu597203933/article/details/44811463