On 2015-04-20 09:21, Alan Gauld wrote:
On 20/04/15 08:44, Jim Mooney wrote:
I can't seem to get my head around this 'simple' book example of
binary-to-decimal conversion, which goes from left to right:

B = '11011101'
I = 0
while B:
     I = I * 2 + int(B[0])
     B = B[1:]
...
Both methods work but I just can't see how the first one does.

The key is that the result gets multiplied by 2 each time
so for an N bit number the leftmost digit winds up being
effectively 2**N, which is what you want.

Shouldn't that be 2**(N-1)?
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to