On 20 April 2015 at 08:44, Jim Mooney <cybervigila...@gmail.com> 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 happens. To begin I is zero and B is the full string. Consider B_orig to be the original string. After the first iteration we have that I = B[0] and B = B_orig[1:] then I = 2*B[0] + B[1] and B = B_orig[2:] then I = 2*(2*B[0] + B[1]) + B[2] = 4*B[0] + 2*B[1] + B[2] then I = 8*B[0] + 4*B[1] + 2*B[2] + B[3] and eventually I = 128*B[0] + 64*B[1] + ... + 2*B[6] + B[7] which is the desired result. Oscar _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor