Skip to content

Commit 23955f6

Browse files
authored
Create smallest-number-with-all-set-bits.cpp
1 parent 5dad99a commit 23955f6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
// bit manipulation
5+
class Solution {
6+
public:
7+
int smallestNumber(int n) {
8+
const auto& bit_length = [](auto x) {
9+
return (x ? std::__lg(x) : -1) + 1;
10+
};
11+
12+
return (1 << bit_length(n)) - 1;
13+
}
14+
};

0 commit comments

Comments
 (0)