🔥 Find the Smallest Number with Only Set Bits 🔢
Intuition \uD83D\uDCA1  \n\nThe problem revolves around finding the smallest number greater than or equal to a given number n where the binary representation of
# Intuition \uD83D\uDCA1  \n\nThe problem revolves around finding the smallest number greater than or equal to a given number `n` where the binary representation of the number contains only `1`s.  \n\n1. **Key Insight**:  \n   - Numbers with only `1`s in their binary representation (e.g., 1, 3, 7, 15) follow the form $$ 2^b - 1 $$, where `b` is the total number of bits.  \n   - The smallest such number greater than or equal to `n` will have a binary representation of all set bits and at least `b` bits where $$ b = \\lceil \\log_2(n + 1) \\rceil $$.  \n\n2. **Approach**:  \n   - Determine the number of bits `b` needed to represent `n`.  \n   - Calculate the result as $$ 2^b - 1 $$.\n\n---\n\n# Approach \uD83D\uDEE0\uFE0F  \n\n1. Compute the number of bits `b` using the formula `b = log2(n) + 1`.  \n2. Return the value $$ (1 << b) - 1 $$, which represents $$ 2^b - 1 $$ with all bits set.  \n\n---\n\n# Complexity \u23F3  \n\n- **Time Complexity**:  \n  $$ O(1) $$ since the calculations involve constant-time operations.  \n\n- **Space Complexity**:  \n  $$ O(1) $$ as no extra space is used.  \n\n---\n![c1ee5054-dd07-4caa-a7df-e21647cfae9e_1709942227.5165014.png](https://assets.leetcode.com/users/images/7865b3eb-6d8d-42fe-9c51-188ad4821476_1733115321.1447082.png)\n\n\n# Code \uD83D\uDCBB  \n\n```cpp []\nclass Solution {\npublic:\n    int smallestNumber(int n) {\n        int b = log2(n) + 1;           // Calculate the number of bits\n        return (1 << b) - 1;          // Return 2^b - 1\n    }\n};\n```\n\n```java []\nclass Solution {\n    public int smallestNumber(int n) {\n        int b = (int) (Math.log(n) / Math.log(2)) + 1;  // Calculate the number of bits\n        return (1 << b) - 1;                           // Return 2^b - 1\n    }\n}\n```\n\n```python []\nclass Solution:\n    def smallestNumber(self, n: int) -> int:\n        b = n.bit_length()         # Calculate the number of bits\n        return (1 << b) - 1        # Return 2^b - 1\n```\n\n```javascript []\n/**\n * @param {number} n\n * @return {number}\n */\nvar smallestNumber = function(n) {\n    let b = Math.floor(Math.log2(n)) + 1; // Calculate the number of bits\n    return (1 << b) - 1;                 // Return 2^b - 1\n};\n```\n\n---\n\n# Explanation \uD83D\uDCD8  \n\n## Example  \n```plaintext\nInput: n = 6  \nOutput: 7  \n\nExplanation:  \n- Binary of 6 = `110`  \n- Smallest number with all set bits >= 6 is `111` (7 in decimal).  \n\nInput: n = 10  \nOutput: 15  \n\nExplanation:  \n- Binary of 10 = `1010`  \n- Smallest number with all set bits >= 10 is `1111` (15 in decimal).  \n```\n\n---\n\n# Takeaway \uD83D\uDE80  \n\nThis problem showcases how mathematical properties of binary numbers simplify implementation! If this helped, **drop a \uD83D\uDC4D and share the love! \uD83D\uDC96** \n\n![1HBC0324_COV.jpg](https://assets.leetcode.com/users/images/70e39342-85db-4485-8efb-38cc388b370a_1733115349.6659803.jpeg)\n
['Bit Manipulation', 'C++', 'Java', 'Python3', 'JavaScript']