Problem
You are given a positive number n.
Return the smallest number x greater than or equal to n, such that the binary representation of x contains only set bits
Algorithm
Using bit operations, add one bit to the left each time until a number not less than n is found.
Code
python3
class Solution:
def smallestNumber(self, n: int) -> int:
ans, bits = 1, 2
while ans < n:
ans += bits
bits <<= 1
return ans