Let's say I have a very very large python integer, in python 2.7 (though if I need to, I don't mind switching to python 3).
Something bigger than say, 2^100000.
What is the fastest way by which I can find the positions of all 1s in it's binary sequence? (example: 24 would be 11000 ---> = [4,5] (or [5,4].. I don't care about order)
At the moment I am using:
sum = whatever_starting_number
while 1:
val = sum.bit_length()-1
sum -= 2**val
mylist.append(val)
if sum == 0:
break
this is alright, but it's barely even faster than just taking log2 and subtracting that repeatedly. What I would like to actually do is just look at the bits, skip zeros, record positions of 1s, and not even need to modify the original value.
edit: getting multiple answers, really appreciate it. I'll implement them in a couple timeit tests and this will be updated with results by tomorrow.