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?
Related problem: Reverse Integer
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
This problem is not very difficult, mainly the bit operation. Because it is an unsigned number, I can rest assured that the original data is shifted to the right, and the new data gets the new one and then moves left. Finally get the result. The method is the same as the number of symbols.
The code is as follows:
classSolution { Public: uint32_t reversebits (uint32_t N) {uint32_t result=0; intbit; for(inti =0; I < +; ++i) {bit= N &0x01; N>>=1; Result<<=1; Result= Result |bit; } returnresult; }};
It is worth mentioning that >>= and <<= these two symbols are basically not used before, so the first time to write the code missing the = number, the future attention.
Happyleetcode58:reverse Bits