Liam Clarke wrote:
4 is 001 (on a continuum of 2^0 to 2^n), but using the above approach
we get 100.

?? 4 (decimal) is 100 (binary). Not because of how the conversion algorithm works, but because that is how we write numbers. The least-significant digit is always the rightmost digit. 001 is 1 in every number base >= 2.


Actually, generating the digits from the right complicates the algorithm quite a bit. It's hidden in the Python version, but s = str(i % 2) + s is a relatively expensive operation here - it has to copy all of s to make room for the new digit.

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to