Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'python3': 0.05; 'test,': 0.05; 'bits': 0.07; 'counting': 0.09; 'subject:Python3': 0.09; 'subject:set': 0.09; 'def': 0.10; 'decent': 0.16; 'equivalents': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'integers,': 0.16; 'iteration': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'operation.': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:bit': 0.16; 'wrote:': 0.17; 'integer': 0.17; 'tests': 0.18; 'bit': 0.21; 'logical': 0.22; 'ones.': 0.22; "i'd": 0.22; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'operations,': 0.27; 'correct': 0.28; 'this?': 0.28; 'received:192.168.1.3': 0.29; 'probably': 0.29; "i'm": 0.29; 'implement': 0.32; 'received:84': 0.32; 'to:addr:python-list': 0.33; 'there': 0.35; 'but': 0.36; "i'll": 0.36; 'drop': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'number:': 0.65; 'union': 0.66; 'header :Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'light- weight': 0.84; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=YaM/Fntf c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=b2nRVtXOy8EA:10 a=v8yo_-cH_nUA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=ZBik15wfPBEA:10 a=YT6p_khQDkDz5JL7oocA:9 a=wPNLvfGTeEIA:10 a=2kbMbnucTJu--l2P:21 a=Cqp-tzKShvjgRXNP:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 25 Oct 2012 16:18:54 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: bit count or bit set && Python3 References: <5089511E.4090009@earthlink.net> In-Reply-To: <5089511E.4090009@earthlink.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351178334 news.xs4all.nl 6966 [2001:888:2000:d::a6]:35720 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32132 On 2012-10-25 15:47, Charles Hixson wrote: > In Python3 is there any good way to count the number of on bits in an > integer (after an & operation)? > 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, > 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? > There's a nice algorithm for counting the ones. It takes one iteration per set bit: def count_set_bits(number): count = 0 while number: count += 1 number &= number - 1 return count