CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Jan 2006
    Posts
    344

    Angry std::bitset woes

    Apparently you can't construct a bitset without a constant value in the constructor and I was wondering if there was a way around that:

    Example:
    Code:
    		std::cout << std::bitset<50>(123456789) << std::endl;  // legal
    		size_t bits = 50;
    		std::cout << std::bitset<bits>(123456789) << std::endl; // illegal
    Not being able to create a bitset with a variable makes it near useless.

    My actual problem is taking place in a loop and I wish to output the bits for huffman code for each one - and since huffman codes are of variable length I need variable length bitsets. I thought bitset's would be ideal for this situation.

    Thanks, G.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: std::bitset woes


  3. #3
    Join Date
    May 2006
    Posts
    327

    Re: std::bitset woes

    I think std::bitset has another limitation: the maximum number of bits is limited to the size of a long integer (32 bits).

    Maybe you can consider the std::vector< bool > class, which stores each value in a single bit, and does not limit the size of the bitset.
    Last edited by Viorel; November 15th, 2006 at 08:45 AM.

  4. #4
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: std::bitset woes

    or.

    Code:
    	typedef unsigned char	ByteT;
    
    	struct CByteT
    	{
    		union
    		{
    			ByteT Val;
    
    			struct
    			{
    				ByteT Bit0: 1;
    				ByteT Bit1: 1;
    				ByteT Bit2: 1;
    				ByteT Bit3: 1;
    				ByteT Bit4: 1;
    				ByteT Bit5: 1;
    				ByteT Bit6: 1;
    				ByteT Bit7: 1;
    			};
    		};
    	};
    
    std::vector<CByteT>
    Last edited by Mitsukai; November 15th, 2006 at 08:53 AM.

  5. #5
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: std::bitset woes

    It seems that lots of people has problem with that std::bitset has fixed size. As suggested above, you can find dynamic bitset in boost library.
    bitset is NOT limited to 32 bits.
    Before using vector<bool>, read Scott Meyers' Effective STL chapter 18 and consider all points there, maybe they do not affect your case and then you could use this construct.
    The most dirty trick I ever heard about making bitset dynamic is to... supply source file, compiler and linker with your app, and recompile program at runtime Author claims that it works great I cannot find a link now, but I will paste it here as soon as I find it.

    Cheers,
    Hob
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  6. #6
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: std::bitset woes

    Quote Originally Posted by Viorel
    Maybe you can consider the std::vector< bool > class, which stores each value in a single bit, and does not limit the size of the bitset.
    Not sure about the exact details but has something to do while using the iterators of vector containing bool, experts advise against it.
    If there is no love sun won't shine

  7. #7
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: std::bitset woes

    recompiling programs at runtime, the beginning of human-like learning computers

  8. #8
    Join Date
    Jan 2006
    Posts
    344

    Re: std::bitset woes

    Rather not start creating too many dependancies with libraries that others in the company don't have/use. As soon as this program gets in the compile chain and the build machine goes looking for non-existant boost libraries I will have to try to explain it and either argue for it or (most likely) remove the dependancy. Was this one of the problems being addressed in C++0x?

    Also my experience (from about a year ago) is that most of the boost library does not compile on MSVC6 - the last time I tried I got hundreds of error messages so I gave up. Maybe I am just lazy but I prefer to simply download a library, double click the .msi and have it ready to use in seconds than to wait a couple of hours to only compile half of it, the other half failing to build.

    Also the fact that it is just for debugging purposes means I can live with out it.

  9. #9
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: std::bitset woes

    Can't you use vector of bitsets?
    If there is no love sun won't shine

  10. #10
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: std::bitset woes

    what is wrong with my code?

  11. #11
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: std::bitset woes

    Quote Originally Posted by Mitsukai
    what is wrong with my code?
    OP wants a variable number of bits while your code has fixed number of bits
    If there is no love sun won't shine

  12. #12
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: std::bitset woes

    Quote Originally Posted by grahamr (work)
    Rather not start creating too many dependancies with libraries that others in the company don't have/use. As soon as this program gets in the compile chain and the build machine goes looking for non-existant boost libraries I will have to try to explain it and either argue for it or (most likely) remove the dependancy. Was this one of the problems being addressed in C++0x?

    Also my experience (from about a year ago) is that most of the boost library does not compile on MSVC6 - the last time I tried I got hundreds of error messages so I gave up. Maybe I am just lazy but I prefer to simply download a library, double click the .msi and have it ready to use in seconds than to wait a couple of hours to only compile half of it, the other half failing to build.

    Also the fact that it is just for debugging purposes means I can live with out it.
    A couple notes:
    - most boost libraries do compile with VC6. There are only a few that do not.
    - Scott Meyer's includes the boost librariesamong the top five Most Important C++ Software Ever. Considering the other four entries, this is impressive. It's worth getting familiar with it.

    Jeff

  13. #13
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: std::bitset woes

    Quote Originally Posted by miteshpandey
    OP wants a variable number of bits while your code has fixed number of bits
    just put it in a vector and you have dynamic number of bits.

  14. #14
    Join Date
    Jan 2006
    Posts
    344

    Re: std::bitset woes

    Quote Originally Posted by jfaust
    A couple notes:
    - most boost libraries do compile with VC6. There are only a few that do not.
    - Scott Meyer's includes the boost librariesamong the top five Most Important C++ Software Ever. Considering the other four entries, this is impressive. It's worth getting familiar with it.

    Jeff
    My experience with Boost is that the last time I tried to compile it I simply saw reams of error messages ('error count exceeds 100 - stopping compilation').

    I doesn't really matter if Ghandi listed it among the top five most important C++ Software Ever, if my boss (and only other developer) doesn't want it then it's not going in. I've had to bring in my own reference books (his is just MSDN). He's never read LSC++SD despite my constant urging, nor has he read any of the other bibles (Stroustrop, Josuttis, Alexandrescu, GoF, etc.).

    G.

  15. #15
    Join Date
    Jan 2006
    Posts
    344

    Re: std::bitset woes

    Quote Originally Posted by Mitsukai
    just put it in a vector and you have dynamic number of bits.
    No it won't since I would have a multiple of 8 bits. Not much help when I want to display just 5 bits as a string is it?
    Last edited by grahamr (work); November 15th, 2006 at 10:39 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured