Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97aa828

Browse files
committed
2017/3/31
1 parent 71769f2 commit 97aa828

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

No190_ReverseBits.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Reverse bits of a given 32 bits unsigned integer.
2+
3+
// For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
4+
5+
// Follow up:
6+
// If this function is called many times, how would you optimize it?
7+
8+
// Related problem: Reverse Integer
9+
10+
11+
class Solution {
12+
public:
13+
// Add one bits has two methods,
14+
// one is 0 | x
15+
// another one is 1 & x
16+
uint32_t reverseBits(uint32_t n) {
17+
// res is a integer contains all 1s
18+
uint32_t res = 0;
19+
for(int i=0; i<32; i++){
20+
res = res << 1;
21+
res = res | (n&1);
22+
n = n >> 1;
23+
}
24+
return res;
25+
}
26+
27+
28+
};

0 commit comments

Comments
 (0)