I am writing a quick program to decompose a number into powers of 2. Is this an efficient way to do it:
pows=[]
pos = 0
while n>0:
if n%2==1: pows.append(2**pos)
n/=2
pos+=1
I've written this in Python but I'm also interested in how it's done in C++.
I don't know if this is a "smart" way to do it or if it's considered horribly inefficient.