[Tutor] bin to dec conversion puzzlement

Alex Kleider akleider at sonic.net
Mon Apr 20 18:58:37 CEST 2015


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)?


More information about the Tutor mailing list