Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.068 X-Spam-Evidence: '*H*': 0.88; '*S*': 0.02; 'binary': 0.07; 'column': 0.07; 'assuming': 0.09; 'stack,': 0.09; 'cc:addr:python-list': 0.11; 'language,': 0.12; 'stored': 0.12; "(it's": 0.16; '37,': 0.16; 'binary.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'popping': 0.16; 'remainder': 0.16; 'subject:search': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'machine': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'easier': 0.31; 'division': 0.31; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'example,': 0.37; 'list,': 0.38; 'dave': 0.60; 'numbers': 0.61; 'matter': 0.61; 'simply': 0.61; "you're": 0.61; 'save': 0.62; 'kind': 0.63; 'maximum': 0.63; 'reverse': 0.68; "'3'": 0.84; "'3',": 0.84; '2015': 0.84; 'divide': 0.84; 'subject:find': 0.84; '378': 0.91; 'angel': 0.91; 'subject:Best': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=036/95qTvOXJxbGq6Eb2mxqvROC/lAx/8LcOENUW3Gs=; b=w1oqvHZR7voi2dv3QvuKT4BXoNwjyxcSqhcPkiDo3VzjJDmDOOQRj2K4H9idgwPdFO nJxb92KpsebUjg2Q02RB3o5RpvAjKDj+hGHdf+VLrT2dHkA7bn2qQh60Ym7D7KBefJjA tsfUmDyyRr81Q9A0P4MN8NWbWVglJOttV/cVt37gtS8bSvJbwApNin5yE1rkChHtdFU8 uokRoRgb5+TcjFjDAQKhO/Nxuetrv1rF1dBkFua8AJXjSsYYCk9VcWDCFnT5IvArQbqL FtX0OJN7LZDXUm1MRS4t+g2NNOaoRByE0m0Z+6HdMcnMl23sJZDLH3KxmCttnrju8Xzw 8Rig== MIME-Version: 1.0 X-Received: by 10.43.38.144 with SMTP id ti16mr26260129icb.26.1428417271129; Tue, 07 Apr 2015 07:34:31 -0700 (PDT) In-Reply-To: <5523E928.6030306@davea.name> References: <2e3a3c01-20b3-4948-9b32-bd80ed46822b@googlegroups.com> <2d885c21-95c9-4ea1-b82e-d5420d886c8b@googlegroups.com> <5523E928.6030306@davea.name> Date: Wed, 8 Apr 2015 00:34:31 +1000 Subject: Re: Best search algorithm to find condition within a range From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428417273 news.xs4all.nl 2906 [2001:888:2000:d::a6]:37712 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88582 On Wed, Apr 8, 2015 at 12:26 AM, Dave Angel wrote: > For example, if you want to convert 378 to base 10 (it's binary internally), > you'd divide by 10 to get 37, remainder 8. Save the 8, and loop again. > Divide 37 by 10 and get 3, remainder 7. Save the 7. Divide again by 10 and > get 0, remainder 3. Save the 3 > > Now you have '8', '7', '3' So you reverse the list, and get > '3', '7', '8' Technically, it doesn't matter that it's stored in binary. All that matters is that it's stored in some way that you can perform division on. I used to do this kind of thing in assembly language, pushing digits onto the stack, then popping them off afterward. (It's actually easier to make a column of numbers right-justified, as you can simply create a buffer of the right size - assuming you're working with a machine word and can know the maximum size - and populate it from the far end.) But yes, this is the standard way to do base conversions. ChrisA