🔥Beats 100 % 🔥 | Easiest Solution ✔️ | O(1) and O(n) | Bit Manipulation
Intuition\nThe goal is to find the smallest number >= n where all bits in its binary representation are set (e.g., 1, 3, 7, 15, etc.). Starting from n, we check
# Intuition\nThe goal is to find the smallest number `>= n` where all **bits** in its binary representation are set (e.g., 1, 3, 7, 15, etc.). Starting from `n`, we check if the number satisfies the condition `x & (x + 1) == 0`, which is true only if `x` has all **bits set** , binary \n`0111 & 1000 = 0`. If `x` doesn\'t meet this condition, increment it and check again until the condition holds. Once found, return this number as the smallest value meeting the criteria.\n\n# Approach\n`Step 1` **Understanding the Bitwise Condition :**\n- `x & (x + 1) == 0` is true only when `x` has all bits set in its binary representation. For example:\n    - 3 (binary 11): `3 & 4 = 0`\n    - 7 (binary 111): 7 & 8 = 0\n\n`Step 2` **Iterate and Increment :**\n- Start from n and keep incrementing `x` until the condition is satisfied.\n\n`Step 3` **Return the Result :**\n- Once the condition is met, `x` is guaranteed to be the\n- `smallest number \u2265 n` with all bits set. Return it.\n\n# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n```java []\nclass Solution \n{\n    public int smallestNumber(int n) \n    {\n        int x = n;\n        \n        // Step 1: Check if x has all bits set\n        while ((x & (x + 1)) != 0) \n        { \n            x++; // Step 2: Increment x until the condition is met\n        }\n        \n        return x; // Step 3 : Return Smallest Number with All Set Bits \n    }\n}\n```
['Bit Manipulation', 'Bitmask', 'Java']