Skip to content

Commit 1336497

Browse files
committed
Create reverse-bits.cpp
1 parent 198c5cf commit 1336497

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

C++/reverse-bits.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(logn) = O(32)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
uint32_t reverseBits(uint32_t n) {
7+
uint32_t result = 0;
8+
int count = 32;
9+
while (count--) {
10+
result <<= 1;
11+
result |= n & 1;
12+
n >>= 1;
13+
}
14+
return result;
15+
}
16+
};

0 commit comments

Comments
 (0)