Skip to content

add a bitequal() function#8

Closed
bgruening wants to merge 0 commit intoilanschnell:masterfrom
bgruening:master
Closed

add a bitequal() function#8
bgruening wants to merge 0 commit intoilanschnell:masterfrom
bgruening:master

Conversation

@bgruening
Copy link

I added a bitequal() function to calculate the same as (a & b).count().

Thanks for this great library.
Björn

@bgruening
Copy link
Author

Hi Ilan,

any chance to get this merged?
Thanks,
Bjoern

@bgruening
Copy link
Author

Is there anything I can do to get this PR merged?

@bgruening bgruening changed the title Hi! add a bitequal() function Apr 13, 2015
@andre-merzky
Copy link

Hey @bgruening,

this repo has been silent for quite a while, and we thus created a fork at https://github.com/diamondman/bitarray/.

If you are interested, please feel free to transplant your pull request to the forked repo, we would be very happy to begin the code review and merge into bitarray before pushing out a new release. If you do not have the time to do so, we would kindly ask your permission to do the PR transfer our-self. If you could ping back in the next couple of days, one way or the other, that would be great.

Many thanks!

@bgruening
Copy link
Author

@andre-merzky thats great to hear! I'm travelling the next days, so if you can take this PR over feel free to do so. Hopefully, it's useful!

@andre-merzky
Copy link

Thanks for the note, @bgruening - we'll do that then, and will ping back. Take care :)

@diamondman
Copy link

diamondman#9

@andre-merzky
Copy link

andre-merzky commented Nov 22, 2016

Hmm, isn't count() exactly what you are looking for?

#!/usr/bin/env python

import bitarray
import random
import time
import sys

n  = 1024*1024*1024
ba = bitarray.bitarray(n)

t_0 = time.time()
for i in range(1000*1000):
    ba.setrange(i*1000, i*1000+999, True)
t_1 = time.time()
print ba.count()
t_2 = time.time()
print 'timer 1: %5.2f' % (t_1 - t_0)
print 'timer 2: %5.2f' % (t_2 - t_1)

gives

$ python ./test.py
1000000000
timer 1:  0.53
timer 2:  0.05

On Tue, Nov 22, 2016 at 1:51 PM, Rémi Adon notifications@github.com wrote:

Hi everyone,
I didn't want to create new issue because I think my question is related.

I would like to get the cardinality of a bitarray, i.e the number of bits
set to 1, as defined in Java's BitSet class
https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html#cardinality()

using bitequal I could try something like:

b = bitarray.bitarray(bitarray.bitarray('1') * my_bitarray.length)
cardinality = bitarray.bitequal(my_bitarray, b)

or I could simply use sum:

cardinality = sum(1 for i in my_bitarray if i)

I did not try bitequal but sum is not that fast (between 10 and 50 ms for
1 million bits on my machine), I mean not that fast if i need to
recompute/use it many times.

Is there a faster way to compute the cardinality ?

@remiadon
Copy link

remiadon commented Nov 22, 2016

ow thank you @andre-merzky , my bad

@pull pull bot closed this Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants