Determining combination of bits

Terry Reedy tjreedy at udel.edu
Mon Nov 8 15:27:47 EST 2004


"Sean Berry" <sean at buildingonline.com> wrote in message 
news:x3Qjd.121786$hj.41260 at fed1read07...
> and I am given some numbers, say 22, 25, and 9.  I want to determine the 
> keys, powers of 2, that comprise the number.
> How do I get these keys?

if n%2: print 'has a positive one bit'

n//2 == n>>1 deletes that bit

keep track of divisions/shifts and stop when n == 0

Terry J. Reedy






More information about the Python-list mailing list