initializing bitset with variable number of bits

Discussion in 'C++' started by zacariaz@gmail.com, May 11, 2007.

  1. Guest

    The subject isnt very clear, but ill do my best to explain.

    In a class in need a bitset which size is defined by the constructor,
    e.g. something like this:

    class Example {
    std::bitset<?>Bs;
    public:
    Example(int input) {
    ? = input;
    }
    };

    Now, my knowledge in c++ is simply to limited to work around this. I
    know that in order to use a variable to initialize a bitset, it has to
    be constant, but allso the constant variable has to be initialized
    imidiadly, and ofcourse it all has to be done in the right order,
    otherwise nothing will make sence or work for that matter. Im confused
    and i hope you canhelp.

    Regards
    , May 11, 2007
    #1
    1. Advertising

  2. wrote:
    > The subject isnt very clear, but ill do my best to explain.
    >
    > In a class in need a bitset which size is defined by the constructor,
    > e.g. something like this:
    >
    > class Example {
    > std::bitset<?>Bs;


    Since the size of the bitset is part of its _type_ definition, bitsets
    with different sizes would mean that your 'Example' would have members
    of different types. That means those are different 'Example' _types_.
    You can only achieve that by making 'Example' a template.

    > public:
    > Example(int input) {
    > ? = input;
    > }
    > };
    >
    > Now, my knowledge in c++ is simply to limited to work around this.


    Actually, C++ is simply limited, not your knowledge. C++ does not
    allow to have the same _class_ to contain different types of members
    depending on some run-time condition.

    > I
    > know that in order to use a variable to initialize a bitset, it has to
    > be constant, but allso the constant variable has to be initialized
    > imidiadly, and ofcourse it all has to be done in the right order,
    > otherwise nothing will make sence or work for that matter. Im confused
    > and i hope you canhelp.


    template<size_t n>
    class Example {
    std::bitset<n> Bs;
    public:
    ...
    };

    ....
    Example<12> e12;
    ....

    Now, _types_ have to be fixed at the compile time. You cannot create
    types during run-time. That's the main limitation.

    Perhaps you can roll your own "bitset", instead of using the standard
    template... Think 'std::vector<bool>', which has dynamic _size_.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 11, 2007
    #2
    1. Advertising

  3. Guest

    On 12 Maj, 00:23, "Victor Bazarov" <> wrote:
    > wrote:
    > > The subject isnt very clear, but ill do my best to explain.

    >
    > > In a class in need a bitset which size is defined by the constructor,
    > > e.g. something like this:

    >
    > > class Example {
    > > std::bitset<?>Bs;

    >
    > Since the size of the bitset is part of its _type_ definition, bitsets
    > with different sizes would mean that your 'Example' would have members
    > of different types. That means those are different 'Example' _types_.
    > You can only achieve that by making 'Example' a template.
    >
    > > public:
    > > Example(int input) {
    > > ? = input;
    > > }
    > > };

    >
    > > Now, my knowledge in c++ is simply to limited to work around this.

    >
    > Actually, C++ is simply limited, not your knowledge. C++ does not
    > allow to have the same _class_ to contain different types of members
    > depending on some run-time condition.
    >
    > > I
    > > know that in order to use a variable to initialize a bitset, it has to
    > > be constant, but allso the constant variable has to be initialized
    > > imidiadly, and ofcourse it all has to be done in the right order,
    > > otherwise nothing will make sence or work for that matter. Im confused
    > > and i hope you canhelp.

    >
    > template<size_t n>
    > class Example {
    > std::bitset<n> Bs;
    > public:
    > ...
    >
    > };
    >
    > ...
    > Example<12> e12;
    > ...
    >
    > Now, _types_ have to be fixed at the compile time. You cannot create
    > types during run-time. That's the main limitation.
    >
    > Perhaps you can roll your own "bitset", instead of using the standard
    > template... Think 'std::vector<bool>', which has dynamic _size_.
    >

    I thought vector<bool> was obsolete, but it might the solution,
    however it is my experience, and i might be wrong, that vector<bool>
    uses one byte of memory per bool, and if that the case it is
    definently not the solution.

    Anyhow, thanks for now.
    , May 11, 2007
    #3
  4. Guest

    > I thought vector<bool> was obsolete...

    I was of course thinking of the bitvector ot bit_vector? cant
    remember, anyway i still seem to remember that vector<bool> takes up
    one byte of memory per bit, which, as i wrote before, is a problem.
    , May 11, 2007
    #4
  5. Bo Persson Guest

    wrote:
    :: On 12 Maj, 00:23, "Victor Bazarov" <> wrote:
    ::: wrote:
    :::: The subject isnt very clear, but ill do my best to explain.
    :::
    :::: In a class in need a bitset which size is defined by the
    :::: constructor, e.g. something like this:
    :::
    :::: class Example {
    :::: std::bitset<?>Bs;
    :::
    ::: Since the size of the bitset is part of its _type_ definition,
    ::: bitsets with different sizes would mean that your 'Example' would
    ::: have members of different types. That means those are different
    ::: 'Example' _types_. You can only achieve that by making 'Example' a
    ::: template.
    :::
    :::: public:
    :::: Example(int input) {
    :::: ? = input;
    :::: }
    :::: };
    :::
    :::: Now, my knowledge in c++ is simply to limited to work around this.
    :::
    ::: Actually, C++ is simply limited, not your knowledge. C++ does not
    ::: allow to have the same _class_ to contain different types of members
    ::: depending on some run-time condition.
    :::
    :::: I
    :::: know that in order to use a variable to initialize a bitset, it
    :::: has to be constant, but allso the constant variable has to be
    :::: initialized imidiadly, and ofcourse it all has to be done in the
    :::: right order, otherwise nothing will make sence or work for that
    :::: matter. Im confused and i hope you canhelp.
    :::
    ::: template<size_t n>
    ::: class Example {
    ::: std::bitset<n> Bs;
    ::: public:
    ::: ...
    :::
    ::: };
    :::
    ::: ...
    ::: Example<12> e12;
    ::: ...
    :::
    ::: Now, _types_ have to be fixed at the compile time. You cannot
    ::: create types during run-time. That's the main limitation.
    :::
    ::: Perhaps you can roll your own "bitset", instead of using the
    ::: standard template... Think 'std::vector<bool>', which has dynamic
    ::: _size_.
    :::
    :: I thought vector<bool> was obsolete, but it might the solution,
    :: however it is my experience, and i might be wrong, that vector<bool>
    :: uses one byte of memory per bool, and if that the case it is
    :: definently not the solution.
    ::
    :: Anyhow, thanks for now.

    No, vector<bool> uses one bit per element, which IS its problem. This might
    be good for you, but in general we would expect a vector<bool> to store
    bools.


    Bo Persson
    Bo Persson, May 12, 2007
    #5
  6. xman Guest

    > In a class in need a bitset which size is defined by the constructor,
    > e.g. something like this:
    >
    > class Example {
    > std::bitset<?>Bs;
    > public:
    > Example(int input) {
    > ? = input;
    > }
    >
    > };


    Perhaps you can try dynamic_bitset in boost library.
    http://www.boost.org/libs/dynamic_bitset/dynamic_bitset.html
    xman, May 12, 2007
    #6
  7. BobR Guest

    <> wrote in message
    news:...
    > > I thought vector<bool> was obsolete...

    >
    > I was of course thinking of the bitvector ot bit_vector? cant
    > remember, anyway i still seem to remember that vector<bool> takes up
    > one byte of memory per bit, which, as i wrote before, is a problem.
    >


    Here is a little snippet I was playing with. Maybe it will give you an idea.
    (You seem 'hep' enough to figure out the #includes. <G>)

    { // main or func
    using std::cout; // for NG posts
    bool bl(true);
    std::vector<std::bitset<1> > VecBit( 7, 1 );
    // note 'bitset' does not have the '.at()' member.
    if( bl == VecBit.at(0)[0] ){
    cout<<"VecBit.at(0)="<<VecBit.at(0)<<std::endl;
    cout<<" ="<<std::boolalpha<<VecBit.at(0)[0]
    <<std::endl;
    }
    // out: VecBit.at(0)=1
    // out: =true

    // Example (stl docs)
    std::bit_vector V(5);
    V[0] = true;
    V[1] = false;
    V[2] = false;
    V[3] = true;
    V[4] = false;
    for(std::bit_vector::iterator i( V.begin() ); i < V.end(); ++i){
    cout<<" "<<std::boolalpha<<(*i)<<std::endl;
    }
    cout <<std::endl;
    }

    Of course, 'vector<bitset<> >' is WAY more inefficient than 'vector<bool>',
    but I left it in there just to show it could be done. <G>
    'std::bit_vector' sounds like what you want.

    --
    Bob R
    POVrookie
    BobR, May 12, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DamonChong
    Replies:
    5
    Views:
    484
    DamonChong
    Apr 20, 2005
  2. sarmin kho
    Replies:
    2
    Views:
    815
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    429
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. Frank Buss
    Replies:
    3
    Views:
    4,028
    Mike Treseler
    Jul 31, 2006
  5. Ninds
    Replies:
    14
    Views:
    701
    W Karas
    Dec 3, 2012
Loading...

Share This Page