What is the most efficient (in terms of speed and space) way to check if a decimal number only has a single '1' in its binary representation without using special functions (math, numpy, etc.)?
e.g. 1 is '001' and 4 is '100'.
I've tried this
binary = "{0:b}".format(value)
if binary.count('1') != 1:
return 1
else:
return 0
I believe this is O(log n) in terms of space and O(n) in terms of speed? Is there a way to do this more efficiently?