Re: [Tutor] bin to dec conversion puzzlement

2015-04-21 Thread Mark Lawrence
On 20/04/2015 21:47, Ben Finney wrote: Jim Mooney writes: 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:] print(I) 221 That is, IMO, a need

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Dave Angel
On 04/20/2015 04:15 PM, Jim Mooney wrote: 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. Alan G Ah, the light dawns once it was restated. It would be even simpler if you could

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Jim Mooney
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. > > Alan G Ah, the light dawns once it was restated. It would be even simpler if you could multiply each element of the binary numbe

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Ben Finney
Jim Mooney writes: > 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:] > > print(I) > >>> 221 That is, IMO, a needlessly confusing way t

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Alan Gauld
On 20/04/15 17:58, Alex Kleider wrote: 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)? Yes, sorry. -- Alan G Author of the Learn to Program web site http

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread eryksun
On Mon, Apr 20, 2015 at 11:21 AM, Alan Gauld wrote: >> >> 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 t

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Alex Kleider
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 wo

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Alan Gauld
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 on

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Oscar Benjamin
On 20 April 2015 at 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:] Follow through the loop and see what ha

Re: [Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Marc Tompkins
On Apr 20, 2015 6:56 AM, "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:] > > print(I) > >>> 221 > > My thought w

[Tutor] bin to dec conversion puzzlement

2015-04-20 Thread Jim Mooney
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:] print(I) >>> 221 My thought was to go from right to left, multiplying digits by successive powers of