<p>On Sep 26, 2011 1:49 AM, <<a href="mailto:bmacinnis@comcast.net">bmacinnis@comcast.net</a>> wrote:<br>
> Is there an equivalent command in python that would immediately provide the number of set bits in a large bit vector/string</p>
<p>Besides what others have said, if you expect the number of bits set to be small, you might just use a set.</p>
<p>bits = set()<br>
# set bit 1000<br>
bits.add(1000)<br>
# clear bit 2000000<br>
bits.discard(2000000)<br>
# test bit 1000<br>
if 1000 in bits:<br>
    pass<br>
# get number of bits set<br>
len(bits)</p>
<p>All of these operations are O(1), but if you expect to set a very large number of bits, then the space trade-off may not be practical.</p>
<p>Cheers,<br>
Ian</p>