Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python3': 0.05; 'test,': 0.05; 'bits': 0.07; 'subject:Python3': 0.09; 'subject:set': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'decent': 0.16; 'equivalents': 0.16; 'integers,': 0.16; 'operation.': 0.16; 'subject:bit': 0.16; 'wrote:': 0.17; 'integer': 0.17; 'tests': 0.18; 'bit': 0.21; 'logical': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; '[1]': 0.27; 'operations,': 0.27; 'correct': 0.28; 'this?': 0.28; 'url:code': 0.29; 'probably': 0.29; "i'm": 0.29; 'thursday,': 0.30; 'implement': 0.32; 'function.': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; "i'll": 0.36; 'october': 0.37; 'drop': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'from:no real name:2**0': 0.60; 'url:p': 0.63; 'union': 0.66; 'light-weight': 0.84 Newsgroups: comp.lang.python Date: Fri, 26 Oct 2012 08:42:54 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=192.94.38.34; posting-account=0vXMwQoAAADCTO6LSb66apqOI0WiEK9C References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 192.94.38.34 MIME-Version: 1.0 Subject: Re: bit count or bit set && Python3 From: casevh@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351266184 news.xs4all.nl 6932 [2001:888:2000:d::a6]:53126 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32221 On Thursday, October 25, 2012 7:56:25 AM UTC-7, Charles Hixson wrote: > In Python3 is there any good way to count the number of on bits in an > integer (after an & operation)? You may want to look at gmpy2[1] and the popcount() function. > > Alternatively, is there any VERY light-weight implementation of a bit > set? I'd prefer to use integers, as I'm probably going to need > thousands of these, if the tests work out. But before I can test, I > need a decent bit counter. (shift, xor, &, and | are already present > for integer values, but I also need to count the number of "true" items > after the logical operation. So if a bitset is the correct approach, > Whether or not gmpy2 is considered light-weight is debateable. :) > I'll need it to implement those operations, or their equivalents in > terms of union and intersection.) > > > > Or do I need to drop into C for this? > [1] http://code.google.com/p/gmpy/ > > > -- > > Charles Hixson