Reverse Bits
Reverse bits of a given the unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 ( Represented in binary as00111001011110000010100101000000).
Followup:
If This function is called many times, what would you optimize it?
Converts the binary of a number to the inverse of the number.
The lowest bit of the number, as the highest bit (2^31), and then solve it in turn.
Example:for 0000..........10ans = 0ans + = 0* (2^31)ans + = 1* (2^30)......
Class Solution {public: uint32_t reversebits (uint32_t N) { uint32_t i = 1, ans = 0,m = 31;//uint32_t cannot be used INT
while (M-) {// will i = 2^31 i <<= 1; } m = +; while (n) { //M-- with m--and n can be ans + = (n&1) *i; I >>= 1; n >>= 1; } return ans; };
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode-190-reverse Bits