LeetCode OJ:Reverse Bits(旋转bit位)
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).
注意应该做到平台无关性,需要做到这样的话应该声明一个uint32量然后一直向左移,知道得到 0 就可以得到uint32的位数大小 :
1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 uint32_t result = 0; 5 for(uint32_t i = 1; i != 0; i <<= 1){ 6 result <<= 1; 7 result |= (n & 1); 8 n >>= 1; 9 } 10 return result; 11 } 12 };