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


Groups > comp.lang.python > #27519

Re: How to convert base 10 to base 2?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'binary': 0.05; 'subject:How': 0.09; 'byte,': 0.09; 'chime': 0.09; 'encode': 0.09; 'minus': 0.09; 'negative.': 0.09; 'received:mail- lpp01m010-f46.google.com': 0.09; 'subtract': 0.09; 'aug': 0.13; 'result.': 0.15; '111111101': 0.16; '127': 0.16; '255': 0.16; 'binary.': 0.16; 'mixture': 0.16; 'sign,': 0.16; 'width,': 0.16; 'mon,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'byte': 0.17; 'otherwise,': 0.20; 'trying': 0.21; 'bit': 0.21; "i'd": 0.22; 'cheers,': 0.23; 'specifically': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fixed': 0.28; 'fine': 0.28; 'represent': 0.28; 'arithmetic': 0.29; 'decimal': 0.29; 'usually': 0.30; 'e.g.': 0.30; 'received:209.85.215.46': 0.30; 'could': 0.32; 'word.': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'consistent': 0.35; 'so,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'positive': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'leading': 0.61; 'between': 0.63; 'more': 0.63; 'within': 0.64; '20,': 0.65; 'topic,': 0.78; 'to:name:python': 0.84; 'joel': 0.91; '000': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=0byAfSO9EI0SOb4ePT/1pOcFJ3meg/QA6i2yxXdZNv4=; b=w92Mv/4vSX3lhnTBMek9Wjwdyn5lJacOfytPXprtIJ3/iA4pa1G58BJn9OQuLgfrt+ yDsrpDkv3ca//ovBG67RMJRiqUGcukoAcnrCKrX6HFfilSf1TyHtm4yTyompTvwciHdN G6DuyizU0a1zrvgn+is9eKtUmhfIPdOy51eLERZOycX9+Uf+U8siWwPqInVRBYSGLaON /gHS7WaTO85tEAOkcIgqehXFHXxeRE3M8yIojhSdQ4Mg8nwFDCv2IuLQ0Ipet+2+LfYm HWWOgsBkFnkuEqMUWktuoIkBTi/m1ZOn6AoQvKW+wxGAIq08A4dH10ViAm58zorHI1GV s2+Q==
MIME-Version 1.0
In-Reply-To <CAPM-O+y9Tn_uhe4TD8F47_vh48nKZbAj5yqyYg7c6DrOkMH2PA@mail.gmail.com>
References <1cedbf80-117b-48aa-a9f2-754293203408@googlegroups.com> <28b3f583-fad1-46da-a6b5-933f966eb401@googlegroups.com> <50324F3A.7040001@sequans.com> <uvr4385905sjupcdb6mkee2jf4gq7tjlch@invalid.netcom.com> <CAPM-O+y9Tn_uhe4TD8F47_vh48nKZbAj5yqyYg7c6DrOkMH2PA@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 20 Aug 2012 12:10:18 -0600
Subject Re: How to convert base 10 to base 2?
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.3574.1345486250.4697.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345486250 news.xs4all.nl 6950 [2001:888:2000:d::a6]:49521
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27519

Show key headers only | View raw


On Mon, Aug 20, 2012 at 11:57 AM, Joel Goldstick
<joel.goldstick@gmail.com> wrote:
> 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!

The main reason to use two's complement is when you need to encode the
negative sign of the number as a bit in a format of fixed width, e.g.
within a byte or word.  In a string representation, unless you are
specifically trying to represent computer memory, it is usually better
to just use a minus sign, to be more consistent with how we usually
represent numerals.

Otherwise, note that complement representations are not specific to
binary.  For example, with decimal numbers we could use "ten's
complement": individually subtract each digit from 9, and add 1 to the
result.  A leading digit between 5 and 9 would be considered negative.

So, for example:
-(042)  -->  958
-(958)  -->  042

958 + 042 == 000

Cheers,
Ian

Back to comp.lang.python | Previous | NextPrevious 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