37

I want to make a bitset in C++. I did a bit of research. All examples I found where like this:

bitset<6> myBitset;
// do something with it

But I don't know the size of the bitset when I define the variable in my class:

#include <bitset>
class Test
{
public:
     std::bitset *myBitset;
}

This won't compile...

And initializing like this also doesn't work:

int size = getDependentSizeForBitset();
myBitset = new bitset<size>();
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 3
    I have no idea what this is for, but you could always use a vector if it won't be too large. This is especially a good option if you're working in an environment which doesn't allow boost (ie. a crappy job). – Cam Jun 28 '10 at 17:41
  • 2
    That should be an answer, not a comment. – graham.reeds Jun 28 '10 at 17:44
  • 2
    std::vector is not only larger, but significantly slower (~5x) in the application that drove me to this question. – sdenham Jul 11 '17 at 13:50

7 Answers7

30

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.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • 5
    +1. It's a shame that vector has that specialization instead of just a separate class all together. It's not like it made it any easier on the vendors to implement it as a specialization: just seems downright silly. – stinky472 Jun 28 '10 at 17:46
  • yep. Unfortunately, I believe the committee has resisted all proposals to deprecate it. – jalf Jun 28 '10 at 18:33
8

Use Boost::dynamic_bitset

Thomas Jones-Low
  • 7,001
  • 2
  • 32
  • 36
1

You should check out boosts dynamic_bitset.

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
0

What you are saying at the beginning is not true. The "examples you found" did not look as you posted. It is impossible to use a non-constant value to parametrize a template. So, your first example is invalid. Only constant expressions can serve as non-type arguments for a template. I.e. the non-type argument has to be a compile-time constant.

Of looks like you want to create a bitset whose size is not a compile-time constant. In this case the bitset template is out of question. You need an implementation of run-time sized bitset. For example, you can use std::vector<bool> - in many (if not all) implementations this template is specialized to implement a packed array of boolean values, where each element occupies one bit (as opposed to an bool object).

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

bitset requires size as a template parameter, meaning the size has to be capable of being determined at compile-time. It cannot be based on a runtime condition, like user input.

For that, you should look into std::vector or boost::dynamic_bitset. std::vector is a specialized template instantiation that uses one bit per element. Unlike bitset, it can be dynamically sized.

stinky472
  • 6,737
  • 28
  • 27
0

If you are solving a coding problem, then one possible idea is: -

  1. Define the bitsets with the maximum possible size according to given constraints.

  2. Define a bitset "mask" of value ((1 << k) - 1), where 'k' is the desired size.

  3. Instead of directly using the bitsets for computation, use (bitsetName & mask)

// Suppose the maximum size of bitset according to given constraints is 20
#define bst20 bitset<20>

// Define bitset with value ((1 << k) - 1)
bst20 mask((1 << k) - 1);

// Before computation use '&' operator with mask

// Some examples
map.insert(bitsetName & mask);
print(bitsetName & mask);
-6

You can make your class a template to make the std::bitset size undetermined until your class gets instantiated. You can do it like this:

#include <bitset>

template<int size>
class Test
{
public: 
    std::bitset<size> bitset;
    //...
}

Then to use your class you would have to do this:

int exampleSize = 42;
Test<exampleSize> name;