Counting bits in large string / bit vector

casevh casevh at gmail.com
Mon Sep 26 17:38:57 CEST 2011


On Sep 26, 12:56 am, Nizamov Shawkat <nizamov.shaw... at gmail.com>
wrote:
> > Is there an equivalent command in python that would immediately provide the
> > number of set bits in a large bit vector/string
>
> You might be able to achieve this using numpy boolean array and, e.g,
> the arithmetic sum function or something similar.
> There is also another library  http://pypi.python.org/pypi/bitarray
> which resembles numpy's bit array.
>
> Hope it helps,
> S.Nizamov

You can also use gmpy or gmpy2.

>>> a=gmpy2.mpz(123)
>>> bin(a)
'0b1111011'
>>> gmpy2.hamdist(a,0)
6
>>>

casevh



More information about the Python-list mailing list