Set all bits in n | C++, Python, Java
Approach\n Describe your approach to solving the problem. \nSet all the bits in n\'s binary representation to 1.\n\nFor the python version, 2^k-1 is a number wi
# Approach\n<!-- Describe your approach to solving the problem. -->\nSet all the bits in n\'s binary representation to 1.\n\nFor the python version, $$2^k-1$$ is a number with all `k` set bits.\n# Complexity\n- Time complexity: $$O(log(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int smallestNumber(int n) {\n        for (int bit = 1; bit < n; bit <<= 1)\n            n |= bit;\n        return n;\n    }\n};\n```\n```python []\nclass Solution:\n    def smallestNumber(self, n: int) -> int:\n        k = 0\n        while 1 << k <= n:\n            k += 1\n        return (1 << k) - 1\n```\n```Java []\nclass Solution {\n    public int smallestNumber(int n) {\n        int bit = 1;\n        while (bit < n)\n            bit = (bit << 1) | 1;\n        return bit;\n    }\n}\n```\n
['Bit Manipulation', 'C', 'Python', 'C++', 'Java', 'Python3']