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


Groups > comp.lang.python > #91510

Re: How to do integers to binary lists and back

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <johnhpote@o2.co.uk>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'binary': 0.05; 'mrab': 0.05; 'feature.': 0.07; 'nicely': 0.07; 'seemed': 0.07; 'subject:How': 0.09; '[1,': 0.09; ']))': 0.09; 'bits.': 0.09; 'integers': 0.09; 'loop.': 0.09; 'python': 0.11; 'algorithm': 0.13; 'question.': 0.13; 'bits,': 0.16; 'expands': 0.16; 'wrote:': 0.16; 'string': 0.17; 'integer': 0.18; '>>>': 0.20; 'bit': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'converting': 0.27; 'becomes': 0.31; 'e.g.': 0.31; 'help,': 0.32; 'subject:lists': 0.32; 'problem': 0.33; 'everyone.': 0.33; 'int': 0.33; 'lists': 0.34; 'to:addr:python- list': 0.35; 'done': 0.35; 'question,': 0.35; 'list': 0.35; 'asking': 0.35; 'but': 0.36; 'being': 0.36; 'text': 0.36; 'there': 0.36; 'thanks': 0.36; 'subject:: ': 0.37; 'thought': 0.37; 'rather': 0.38; 'doing': 0.38; 'offered': 0.38; 'wanted': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'some': 0.40; 'back': 0.61; 'john': 0.61; 'genuine': 0.63; 'great': 0.64; 'forward': 0.65; 'decided': 0.65; 'charset:windows-1252': 0.65; 'homework': 0.84; 'replies.': 0.84; 'route': 0.84; 'years!': 0.84
Date Sat, 30 May 2015 00:18:02 +0100
From John Pote <johnhpote@o2.co.uk>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version 1.0
To python-list@python.org
Subject Re: How to do integers to binary lists and back
References <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> <CAPTjJmpEHQV2PpaZvszuTGn0_GNg0sWV6aLenbQsZrSXCy2YKQ@mail.gmail.com> <mailman.18957.1424524392.18130.python-list@python.org> <mcargl$h3k$1@reader1.panix.com> <mailman.19008.1424611304.18130.python-list@python.org> <cl2g0vFfiq9U1@mid.individual.net> <mailman.19117.1424755795.18130.python-list@python.org> <cl486mFu4l9U1@mid.individual.net> <mailman.19181.1424868605.18130.python-list@python.org> <cl7pmjF7595U1@mid.individual.net> <F521BFFE-506F-4B58-9A15-20D91A019BD0@gmail.com> <CALwzidm3AngMF9aG-gLpYur6CfaADM0YXGU=p0=dv-qqYXmmFA@mail.gmail.com> <8A3659BC-9100-4A3A-9117-47227B3D290B@gmail.com> <CALwzidmgj1y==3iv8Zub+aHyFR3P3okeLBRKu+R92914mDXfqQ@mail.gmail.com> <5522439B-AEFA-400D-9B6F-00613528381F@gmail.com> <555E5A44.20006@o2.co.uk> <555E5CD8.4090704@mrabarnett.plus.com>
In-Reply-To <555E5CD8.4090704@mrabarnett.plus.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.212.1432941587.5151.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432941587 news.xs4all.nl 2913 [2001:888:2000:d::a6]:60861
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91510

Show key headers only | View raw



On 21/05/2015 23:31, MRAB wrote:
> On 2015-05-21 23:20, John Pote wrote:
>> Hi everyone.
>> I recently had the problem of converting from an integer to its
>> representation as a list of binary bits, each bit being an integer 1 or
>> 0, and vice versa. E.G.
>> 0x53
>> becomes
>> [ 0, 1, 0, 1, 0, 0, 1, 1 ]
>>
>> This I wanted to do for integers of many tens, if not hundreds, of bits.
>> Python very nicely expands integers to any size required, great feature.
>>
>> Just wondered if there was a neat way of doing this without resorting to
>> a bit bashing loop.
>>
>> Looking forward to some interesting answers,
>> John
>>
>>
> I don't know how efficient you want it to be, but:
>
> >>> number = 0x53
> >>> bin(number)
> '0b1010011'
> >>> bin(number)[2 : ]
> '1010011'
> >>> list(map(int, bin(number)[2 : ]))
> [1, 0, 1, 0, 0, 1, 1]
>
Thanks for the replies. Interesting that the offered solutions involve 
converting to a binary text string and then the individual chars back to 
ints. I had thought this would be a route to solve this problem but it 
seemed a bit 'heavy' hence I thought it worthwhile asking the question.

My solution to converting a list of 1s and 0s back to an int is
         listLen = len( binList )
         n = sum( [ binList[i]*( 2**(listLen-1 - i) ) for i in 
range(listLen)] )

In response to Ben Finney's question, I haven't done homework for 40 
years! Genuine problem, I had decided that the clearest way to write the 
algorithm I was working on was to use lists of 1s and 0s rather than 
normal ints.

Thanks for the help,
John

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


Thread

Re: How to do integers to binary lists and back John Pote <johnhpote@o2.co.uk> - 2015-05-30 00:18 +0100

csiph-web