Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32225
| Date | 2012-10-26 09:30 -0700 |
|---|---|
| From | Charles Hixson <charleshixsn@earthlink.net> |
| Subject | Re: bit count or bit set && Python3 |
| References | <mailman.2846.1351176984.27098.python-list@python.org> <0a70ae3c-5058-4b12-89c7-30a27aa8baa8@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2896.1351269379.27098.python-list@python.org> (permalink) |
casevh@gmail.com wrote:
> On Thursday, October 25, 2012 7:56:25 AM UTC-7, Charles Hixson wrote:
>> In Python3 is there any good way to count the number of on bits in an
>> integer (after an& operation)?
> You may want to look at gmpy2[1] and the popcount() function.
>
>> 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,
>>
> Whether or not gmpy2 is considered light-weight is debateable. :)
>
>> 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?
>>
> [1] http://code.google.com/p/gmpy/
>
>>
>> --
>>
>> Charles Hixson
I can see many times when that would be useful, but for this particular
case I think that bin(val).count("1") is probably the better solution.
The other options that I need are already available directly in integer
numbers, and I will be surprised if I need more than a 32-bit set, so
integers should be a reasonable approach. It doesn't seem to have the
overhead that I feared a string conversion would have (possibly because
converting an integer to a bit string is trivial), so I don't think
that gmpy would add value to this program.
Next I need to decide about weak pointers, and then shelve vs.
tokyocabinet. (I sort of don't like shelve, because of its use of
pickle, with the attendent security risks. OTOH, the file will be local
to the computer, not going over the net, which minimizes that. Still, I
may decide to reimplement it using ast.literal_eval, as I'm not
intending to store anything that it won't handle.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
bit count or bit set && Python3 Charles Hixson <charleshixsn@earthlink.net> - 2012-10-25 07:47 -0700
Re: bit count or bit set && Python3 rusi <rustompmody@gmail.com> - 2012-10-25 09:13 -0700
Re: bit count or bit set && Python3 Terry Reedy <tjreedy@udel.edu> - 2012-10-25 16:16 -0400
Re: bit count or bit set && Python3 Christian Gollwitzer <auriocus@gmx.de> - 2012-10-25 19:25 +0200
Re: bit count or bit set && Python3 Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-25 12:19 -0600
Re: bit count or bit set && Python3 casevh@gmail.com - 2012-10-26 08:42 -0700
Re: bit count or bit set && Python3 Charles Hixson <charleshixsn@earthlink.net> - 2012-10-26 09:30 -0700
Re: bit count or bit set && Python3 casevh@gmail.com - 2012-10-26 08:42 -0700
csiph-web