Determining combination of bits
Steven Bethard
steven.bethard at gmail.com
Mon Nov 8 15:18:08 EST 2004
Sean Berry <sean <at> buildingonline.com> writes:
>
> I want to determine the keys, powers of 2, that comprise the number.
>
> Ex. 22 = 16+4+2
> 25 = 16+8+1
> 9 = 8+1
> ...etc...
This sounds suspiciously like a homework, so I'm just going to give a hint here:
>>> import math
>>> math.log(22, 2)
4.4594316186372973
>>> 2**int(math.log(22, 2))
16
>>> 2**int(math.log(22 - 16, 2))
4
>>> 2**int(math.log(22 - 16 - 4 - 2, 2))
2
>>>
Hope this is helpful.
Steve
More information about the Python-list
mailing list