Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27515

Re: How to convert base 10 to base 2?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joel.goldstick@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'binary': 0.05; '"c"': 0.07; 'builtin': 0.07; 'subject:How': 0.09; 'python': 0.09; '"a"': 0.09; 'byte,': 0.09; 'chime': 0.09; 'encode': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'aug': 0.13; 'result.': 0.15; '"-"': 0.16; '"0"': 0.16; '"2"': 0.16; '"b"': 0.16; '"d"': 0.16; '"f"': 0.16; "'0'": 0.16; '111111101': 0.16; '127': 0.16; '255': 0.16; 'hex': 0.16; 'mixture': 0.16; 'valueerror,': 0.16; 'mon,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'integer': 0.17; '(not': 0.20; 'bit': 0.21; 'import': 0.21; "i'd": 0.22; 'received:74.125.82.174': 0.23; 'cc:2**0': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; 'skip:" 20': 0.26; '2.6': 0.27; 'message- id:@mail.gmail.com': 0.27; 'fine': 0.28; 'represent': 0.28; '>>>>': 0.29; 'arithmetic': 0.29; 'url:mailman': 0.29; 'function': 0.30; 'url:python': 0.32; 'url:listinfo': 0.32; 'received:74.125.82': 0.33; '+0200,': 0.33; 'url:home': 0.33; 'typically': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'done': 0.34; 'pm,': 0.35; 'subject:?': 0.35; 'add': 0.36; 'but': 0.36; 'received:74.125': 0.36; 'url:org': 0.36; 'subject:: ': 0.38; 'positive': 0.38; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; '20,': 0.65; 'topic,': 0.78; 'dennis': 0.91; 'joel': 0.91
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:to :cc:content-type; bh=Gw9+mUbGY6qeRF3bfhaBms2nlSOLCHYhH01JFQXBrlw=; b=izRRN8D2mjr51GypTeduOFkK9rtTPa5UYGHLtApbwdNHl/ifDUFA62jUW2l39z8CNv /Yod3grreiLPJglpA4WdxDngV1Iq0u2x9zJCqJAYDwuYL/brWkn5H7NZe67b55WI9utX jZKrpw3DiyJHQPVIRvi+IFYqDtvEwE9frZ68H6Rh4hoknbRmuz3WzAbDjdXhznKOlyIh 49uzfDRwlMdPRJal46L5EgQWrhuK0f0P5AAznGkP5JXcHm0k3ThywM/cdy1Uk3Fu/Mq2 s1ybik3fjRIk+0JwKMvswvQKDmbi5XehMVQxosWdBNo4zpMQ/OhZbQkw83AKVI0GrZWk OWuQ==
MIME-Version 1.0
In-Reply-To <uvr4385905sjupcdb6mkee2jf4gq7tjlch@invalid.netcom.com>
References <1cedbf80-117b-48aa-a9f2-754293203408@googlegroups.com> <28b3f583-fad1-46da-a6b5-933f966eb401@googlegroups.com> <50324F3A.7040001@sequans.com> <uvr4385905sjupcdb6mkee2jf4gq7tjlch@invalid.netcom.com>
Date Mon, 20 Aug 2012 13:57:59 -0400
Subject Re: How to convert base 10 to base 2?
From Joel Goldstick <joel.goldstick@gmail.com>
To Dennis Lee Bieber <wlfraed@ix.netcom.com>
Content-Type text/plain; charset=UTF-8
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3571.1345485481.4697.python-list@python.org> (permalink)
Lines 79
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345485481 news.xs4all.nl 6920 [2001:888:2000:d::a6]:37329
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27515

Show key headers only | View raw


On Mon, Aug 20, 2012 at 1:29 PM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
> On Mon, 20 Aug 2012 16:52:42 +0200, Jean-Michel Pichavant
> <jeanmichel@sequans.com> declaimed the following in
> gmane.comp.python.general:
>
>> note that the builtin bin function is not available with python ver < 2.6
>>
>> def Denary2Binary(n):
>>     '''convert denary integer n to binary string bStr'''
>>     bStr = ''
>>     if n < 0:  raise ValueError, "must be a positive integer"
>>     if n == 0: return '0'
>>     while n > 0:
>>         bStr = str(n % 2) + bStr
>>         n = n >> 1
>>     return bStr
>>
>> JM
>>
>> (not my function but I can't remember who I stole from)
>
>         I think I typically have done this by going through a hex
> representation.
>
> H2B_Lookup = {  "0" : "0000",   "1" : "0001",
>                                         "2" : "0010",   "3" : "0011",
>                                         "4" : "0100",   "5" : "0101",
>                                         "6" : "0110",   "7" : "0111",
>                                         "8" : "1000",   "9" : "1001",
>                                         "A" : "1010",   "B" : "1011",
>                                         "C" : "1100",   "D" : "1101",
>                                         "D" : "1110",   "F" : "1111"    }
>
> def I2B(i):
>     sgn = " "
>     if i < 0:
>         sgn = "-"
>         i = -i
>     h = ("%X" % i)
>     return sgn + "".join([H2B_Lookup[c] for c in h])
>
>>>> from i2b import I2B
>>>> I2B(10)
> ' 1010'
>>>> I2B(1238)
> ' 010011100110'
>>>> I2B(-6)
> '-0110'
>>>>
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list

This may be moving off topic, but since you encode -6 as -0110 I
thought I'd chime in on 'two's complement'

with binary number, you can represent 0 to 255 in a byte, or you can
represent numbers from 127 to -128.  To get the negative you
complement each bit (0s to 1s, 1s to 0s), then add one to the result.
So:
3 -->    00000011
~3 ->   111111100
add 1               1
result   111111101

The nice thing about this representation is that arithmetic works just
fine with a mixture of negative and positive numbers.

eg 8 + (-3) ----> 00001000
                       111111101
gives:               00000101
which is 5!

-- 
Joel Goldstick

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to convert base 10 to base 2? gianpycea@gmail.com - 2012-08-20 00:50 -0700
  Re: How to convert base 10 to base 2? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-08-20 01:04 -0700
    Re: How to convert base 10 to base 2? Miki Tebeka <miki.tebeka@gmail.com> - 2012-08-21 09:23 -0700
    Re: How to convert base 10 to base 2? Miki Tebeka <miki.tebeka@gmail.com> - 2012-08-21 09:23 -0700
  Re: How to convert base 10 to base 2? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-20 09:10 +0100
  Re: How to convert base 10 to base 2? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-20 09:26 +0100
  Re: How to convert base 10 to base 2? gianpycea@gmail.com - 2012-08-20 01:57 -0700
    Re: How to convert base 10 to base 2? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-08-20 16:52 +0200
    Re: How to convert base 10 to base 2? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-20 13:29 -0400
    Re: How to convert base 10 to base 2? Joel Goldstick <joel.goldstick@gmail.com> - 2012-08-20 13:57 -0400
    Re: How to convert base 10 to base 2? Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-20 12:00 -0600
      Re: How to convert base 10 to base 2? Paul Rubin <no.email@nospam.invalid> - 2012-08-20 21:05 -0700
    Re: How to convert base 10 to base 2? Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-20 12:10 -0600

csiph-web