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?

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 2012-08-20 12:10 -0600
Subject Re: How to convert base 10 to base 2?
Newsgroups comp.lang.python
Message-ID <mailman.3574.1345486250.4697.python-list@python.org> (permalink)

Show all headers | 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