Title:
Reverse bits of a given the unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 ( Represented in binary as 00111001011110000010100101000000).
Follow up:
If This function is called many times, what would you optimize it?
Tips:
This problem is relatively simple, the test is the bitwise operation, can be aided by the bitset of STL, can also be directly through the bit operation to complete the requirements of the topic.
Code:
Using Bitset:
classSolution { Public: uint32_t reversebits (uint32_t N) {bitset< +>bits (n); inti =0, j = to, TMP; for(; i < J; ++i,--j) {tmp=Bits[i]; Bits[i]=Bits[j]; BITS[J]=tmp; } unsignedLongL =Bits.to_ulong (); return(uint32_t) L; }};
Direct bitwise OPERATION:
class solution { public : uint32_t reversebits (uint32_t N) {uint32_t m = 0 ; for (int i = 0 ; I < 32 ; ++i, n = n >> 1 ) m = (M << ; 1 ) + (N & 1 ); return m; }};
"Leetcode" 190. Reverse Bits