convert bitset to int in c++
Use to_ulong to convert it to unsigned long, then an ordinary cast to convert it to int. int mybit_int; mybit_int = (int)(mybit.to_ulong()); DEMO
Use to_ulong to convert it to unsigned long, then an ordinary cast to convert it to int. int mybit_int; mybit_int = (int)(mybit.to_ulong()); DEMO
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.
There’s nothing in the standard library. Try: http://pypi.python.org/pypi/bitarray
I don’t think there was ever an actual decision to exclude iterators from bitset. Rather, bitset is one of the classes that predates the proposal to add the original Standard Template Library to the C++ standard. When it was designed, essentially none of the standard library included iterators. Then, Stepanov’s library was proposed for addition, … Read more
As selected answer says you can use findAndModify to generate sequential IDs. But I strongly disagree with opinion that you should not do that. It all depends on your business needs. Having 12-byte ID may be very resource consuming and cause significant scalability issues in future. I have detailed answer here.
The following code creates a bit set from a long value and vice versa: public class Bits { public static BitSet convert(long value) { BitSet bits = new BitSet(); int index = 0; while (value != 0L) { if (value % 2L != 0) { bits.set(index); } ++index; value = value >>> 1; } return … Read more
Update It’s been ages since I posted this one, but: I already know that it is easier and clearer than bit-fiddling on an integer, but is it as fast? If you are using bitset in a way that does actually make it clearer and cleaner than bit-fiddling, like checking for one bit at a time … Read more
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