Can string formatting be used to convert an integer to its binary form ?

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Thu Sep 28 21:12:58 EDT 2006


In message <efhril$nrm$1 at lust.ihug.co.nz>, I wrote:

>     "".join([["0", "1"][(1 << i & n) != 0] for i in range(int(math.ceil(math.log(n, 2))) - 1, -1, -1)])

Uh, make that

    "".join([["0", "1"][(1 << i & n) != 0] for i in range(int(math.floor(math.log(n, 2))), -1, -1)])

Need to check those corner cases. :)



More information about the Python-list mailing list