I have been using the Bitset class in Java and I would like to do something similar in C. I suppose I would have to do it manually as most stuff in C. What would be an efficient way to implement?
byte bitset[]
maybe
bool bitset[]
?
I have been using the Bitset class in Java and I would like to do something similar in C. I suppose I would have to do it manually as most stuff in C. What would be an efficient way to implement?
byte bitset[]
maybe
bool bitset[]
?
Make it an array of unsigned int 64.
CCAN has a bitset implementation you can use: http://ccan.ozlabs.org/info/jbitset.html
But if you do end up implementing it yourself (for instance if you don't like the dependencies on that package), you should use an array of ints and use the native size of the computer architecture:
Don't use a specific size (e.g. with uint64 or uint32), let the computer use what it wants to use and adapt to that using sizeof.
As usual you need to first decide what sort of operations you need to perform on your bitset. Perhaps some subset of what Java defines? After that you can decide how best to implement it. You can certainly look at the source for BitSet.java in OpenJDK for ideas.
I recommend my BITSCAN C++ library (version 1.0 has just been released). BITSCAN is specifically oriented for fast bitscan operations. I have used it to implement NP-Hard combinatorial problems involving simple undirected graphs, such as maximum clique (see BBMC algorithm, for a leading exact solver).
A comparison between BITSCAN and standard solutions STL bitset and BOOST dynamic_bitset is available here: http://blog.biicode.com/bitscan-efficiency-at-glance/
You can give my PackedArray code a try with a
bitsPerItem
of1
.It implements a random access container where items are packed at the bit-level. In other words, it acts as if you were able to manipulate a e.g.
uint9_t
oruint17_t
array:Nobody mentioned what the C FAQ recommends, which is a bunch of good-old-macros:
(via http://c-faq.com/misc/bitsets.html)