943,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 408
  • C++ RSS
Sep 2nd, 2010
0

Using variable length variable (bitset)

Expand Post »
Hello.

i needed a variable size bit string. And since bitset provides several operations, i wanted to use it. But is it possible to make it a variable size one?

C++ Syntax (Toggle Plain Text)
  1. class TryBits {
  2. private:
  3. string s;
  4. bitset<n> b; //now i need n = s.size().
  5. };

How can i achieve this?
Thanks
Similar Threads
Reputation Points: 57
Solved Threads: 38
Posting Whiz
myk45 is offline Offline
311 posts
since Sep 2010
Sep 2nd, 2010
0
Re: Using variable length variable (bitset)
I think the best way to achieve this is to wrap the functionality of a bitset with custom functions.

So, to extend your bitset:
C++ Syntax (Toggle Plain Text)
  1. class TryBits
  2. {
  3. // whatever other functionality
  4. public:
  5. extend( const string& ns )
  6. {
  7. // insert a check for ns to be a valid bit string.
  8. // i.e. only 1s and 0s
  9. s += ns;
  10. b = bitset< s.size() >( s );
  11. }
  12. append( bool nbit )
  13. {
  14. bitset< b.count() + 1 > nb;
  15. for( int i=0; i<b.count(); i++ )
  16. nb[i] = b[i];
  17. nb[ b.count() ] = nbit;
  18. b = nb;
  19. }
  20. void remove( int index )
  21. {
  22. // check boundary conditions
  23. bitset< b.count() - 1 > nb;
  24. for( int i=0, j=0; i<b.count(); i++, j++ )
  25. {
  26. if( i == index )
  27. i++;
  28. nb[j] = b[i];
  29. }
  30. b = nb;
  31. }
  32. };

I haven't tested any of this, but I think you could proceed with this kind of an idea. You would probably want to add in a setSize( int n ) function as well.
Reputation Points: 152
Solved Threads: 41
Posting Whiz in Training
dusktreader is offline Offline
255 posts
since Jan 2010
Sep 2nd, 2010
0
Re: Using variable length variable (bitset)
Thanks for the reply dusktreader.
But the problem was, how shall i declare the private member b as?

bitset<2> b;

b = bitset<4> ("1101");
gives compilation error. So, how can i declare my private member, since there is a requirement for a constant to be present.

Thanks.
Reputation Points: 57
Solved Threads: 38
Posting Whiz
myk45 is offline Offline
311 posts
since Sep 2010
Sep 2nd, 2010
0
Re: Using variable length variable (bitset)
Hmmm.... I see the problem. How asinine to make the bitset size controlled by a template parameter.

I think you could go down two roads.

First, you could store your bits in the string exclusively and return a specific bitset when asked:

C++ Syntax (Toggle Plain Text)
  1. template <int bsize>
  2. bitset<bsize> getBits()
  3. {
  4. return bitset<bsize> bits( s );
  5. }

I would personally favor using a vector<bool>. Of course, you'll have to implement your own logic. This would provide all the extensibility you would need.
Reputation Points: 152
Solved Threads: 41
Posting Whiz in Training
dusktreader is offline Offline
255 posts
since Jan 2010
Sep 3rd, 2010
0
Re: Using variable length variable (bitset)
Thanks. i think vector<bool> would be a better option for me, although i would have to add functionality to it.
Reputation Points: 57
Solved Threads: 38
Posting Whiz
myk45 is offline Offline
311 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Insert Graphics.
Next Thread in C++ Forum Timeline: Serialization





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC