Reverse bits of a given the unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represent Ed in binary as 00111001011110000010100101000000).
Follow up:
If This function is called many times, what would you optimize it?
Ideas
The basic idea is to extract the current highest and lowest bits of information, to exchange, and then the highest bit---the lowest bit + +, until the encounter; 32 digits, so 0-15 and 16-31 are respectively operated. Here I first judge the highest bit, if it is 1, then the lowest judgment, if also 1, do not operate, if 0, then the lowest to become 1, this side used the displacement, and then the highest bit to do subtraction, then becomes 0. If the highest bit is 0, do the opposite.
Class Solution {public: uint32_t reversebits (uint32_t n) { int cur; for (int i = +; I >= 16;i--) { if (n& (1<<i)) { cur = n&1<< (31-i); if (cur = = 0) { N-= 1<<i; n + = 1<< (31-i); } } else{ cur = n&1<< (31-i); if (cur! = 0) { n + = 1<<i; n-=1<< (31-i); }}} return n; }};
Leetcode "190. Reverse Bits "