Simple and Easy Solution | ✅Beats 100% | C++| Java | Python | JavaScript
\n\n# \u2B06\uFE0F Upvote if it helps! \u2B06\uFE0F  \n## Connect with me on LinkedIn: Bijoy Sing  \n\n---\n\n\n## Follow me also on Codeforces: Bijoy Sing\n\n\
\n\n# \u2B06\uFE0F Upvote if it helps! \u2B06\uFE0F  \n## Connect with me on LinkedIn: [Bijoy Sing](https://www.linkedin.com/in/bijoy-sing-236a5a1b2/)  \n\n---\n\n\n## Follow me also on Codeforces: [Bijoy Sing](https://codeforces.com/profile/BijoySingh7)\n\n\n---\n\n### **Solution in C++, Python, Java, and JavaScript**\n\n```cpp\nclass Solution {\npublic:\n    int smallestNumber(int n) {\n        int bit = log2(n) + 1; // Calculate the number of bits required\n        int ans = (1 << bit) - 1; // Construct the smallest number with all bits set\n        return ans;\n    }\n};\n```\n\n```python\nclass Solution:\n    def smallestNumber(self, n):\n        bit = n.bit_length()  # Calculate the number of bits required\n        ans = (1 << bit) - 1  # Construct the smallest number with all bits set\n        return ans\n```\n\n```java\nclass Solution {\n    public int smallestNumber(int n) {\n        int bit = (int)(Math.log(n) / Math.log(2)) + 1; // Calculate the number of bits required\n        int ans = (1 << bit) - 1; // Construct the smallest number with all bits set\n        return ans;\n    }\n}\n```\n\n```javascript\nclass Solution {\n    smallestNumber(n) {\n        let bit = Math.floor(Math.log2(n)) + 1; // Calculate the number of bits required\n        let ans = (1 << bit) - 1; // Construct the smallest number with all bits set\n        return ans;\n    }\n}\n```\n\n---\n\n### **Explanation**\nThe goal is to find the smallest integer where all its bits are set to 1, given \\( n \\). \n\n#### Approach:\n1. Calculate the number of bits needed to represent \\( n \\).  \n   - In C++, Python, JavaScript: Use `log2(n)` and add 1.\n   - In Java: Use `Math.log(n) / Math.log(2)` and add 1.\n2. Construct the smallest number with all bits set using bit manipulation:  \n   \\( (1 << \\text{bit}) - 1 \\).  \n   This shifts 1 left by `bit` positions, filling it with 1\'s, and subtracts 1 to create the final result.\n\n---\n\n### **Complexity Analysis**\n- **Time Complexity**: \\( O(1) \\)  \n  All operations (logarithm and bit manipulation) take constant time.  \n\n- **Space Complexity**: \\( O(1) \\)  \n  No additional space is required.  \n\n---\n\n### **If you liked the solution, don\'t forget to upvote! \uD83D\uDE0A**
['Bit Manipulation', 'Bitmask', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']