bit_ceil 1 line||beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nUse bit_ceil (C++20). That is the smallest 2 powers greater or equal to n\n# Approach\n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse bit_ceil (C++20). That is the smallest 2 powers greater or equal to n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- If bit_ceil(n)=n, n is a 2 power, return 2*n-1\n- otherwise return bit_ceil(n)-1\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nAt most $O(\\log n)$ maybe $O(32)=O(1)$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code||0ms beats 100%\n```cpp []\nclass Solution {\npublic:\n    int smallestNumber(unsigned n) {\n        return (bit_ceil(n)==n)?(n<<1)-1:bit_ceil(n)-1;\n    }\n};\n```\n# Code variant\n```\nclass Solution {\npublic:\n    int smallestNumber(unsigned n) {\n        return bit_ceil(n+1)-1;\n    }\n};\n```
['Bit Manipulation', 'C++']