bit count or bit set && Python3

Dan Stromberg drsalists at gmail.com
Thu Oct 25 12:35:10 EDT 2012


On Thu, Oct 25, 2012 at 9:24 AM, Mark Lawrence <breamoreboy at yahoo.co.uk>wrote:

> On 25/10/2012 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?
>>
>>
> If needed bitarray and bitstring are available on pypi.
>

There's also my bits_mod.py:  http://stromberg.dnsalias.org/svn/bits/trunk/

I separated it out of my bloom filter code recently, to facilitate a prime
number sieve.

Behind the scenes it's an array of int's.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121025/a1d96a50/attachment.html>


More information about the Python-list mailing list