How to implement a bitset in C?

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: #define WORD_BITS (8 * sizeof(unsigned int)) unsigned int * bitarray … Read more

Java BitSet Example

For the specific problem you mentioned: when you called bits2.set(1000001), you set the one millionth and first bit to true. Then when you intersected with bits1, which had the one million, 111 thousand, and 111st bit set, they had no bits in common. I think what you meant to do was bits2.set(0); // set the … Read more

Define bitset size at initialization?

Boost has a dynamic_bitset you can use. Alternatively, you can use a vector<bool>, which (unfortunately) is specialized to act as a bitset. This causes a lot of confusion, and in general is considered a bad idea. But that’s how it works, so if that’s what you need, you might as well use it, I suppose.