Re: Counting bits in large string / bit vector

2011-09-26 Thread Ian Kelly
On Sep 26, 2011 1:49 AM, wrote: > Is there an equivalent command in python that would immediately provide the number of set bits in a large bit vector/string Besides what others have said, if you expect the number of bits set to be small, you might just use a set. bits = set() # set bit 1000 bit

Re: Counting bits in large string / bit vector

2011-09-26 Thread casevh
On Sep 26, 12:56 am, Nizamov Shawkat 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 simil

Re: Counting bits in large string / bit vector

2011-09-26 Thread Arnaud Delobelle
b = numpy.zeros(10**7, dtype=bool) for x in 3, 4, 6: b[10**x] = True > ... b.sum() > 3 Without numpy: >>> counts = [bin(i).count('1') for i in range(256)] >>> bytes = b"hello python"*10 >>> len(bytes)*8 960 >>> sum(map(counts.__getitem__, bytes)) 480 Pretty fast as wel

Re: Counting bits in large string / bit vector

2011-09-26 Thread Peter Otten
bmacin...@comcast.net wrote: > In Perl I can create a large bit vector as follows: > vec($bitmap,1000,1) = 0;# this will create a bit string of all > zeros > To set bits I may using commands like: > vec($bitmap,1000, 1) = 1 # turn on bit 1000 > vec($bitmap,1, 1) =

Re: Counting bits in large string / bit vector

2011-09-26 Thread Nizamov Shawkat
> 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.pytho

Counting bits in large string / bit vector

2011-09-26 Thread bmacinnis
In Perl I can create a large bit vector as follows: vec($bitmap,1000,1) = 0;# this will create a bit string of all zeros To set bits I may using commands like: vec($bitmap,1000, 1) = 1 # turn on bit 1000 vec($bitmap,1, 1) = 1# turn on bit 1