CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2010
    Posts
    32

    Lightbulb How to save memory by using bit-precision in C++?

    Hi, I have met a confusing problem.
    For example, one square can have 5 states: labeled 1, labeled 2, labeled 3, empty, blocked.
    They theoretically I could just use 3 bits to hold the information about that square.

    That would save a lot of memory. Think about how much memory will I have to use if I use integer 1,2,3,4,5 to represent those five states. I would be 4bytes=32bits.

    But how can I manipulate the bits and store what in information in terms of bits in C++?
    I've tried bitset, but it yields weird result while I was doing the following.

    Code:
    bitset<3>  how_is_it_going;
    size_t sz1 sizeof(how_is_it_going);
    size_t sz2 sizeof(how_is_it_going);
    cout<<sz1;
    cout<<sz2;
    The first one returns 4 and the second even returns 8! I suppose it's in bytes, It just doesn't make sense.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to save memory by using bit-precision in C++?

    What compiler accepts that code? Modifying it to
    Code:
    bitset<3>  how_is_it_going;
    size_t sz1 = sizeof(how_is_it_going);
    size_t sz2 = sizeof(how_is_it_going);
    cout<<sz1;
    cout<<sz2;
    and building it in MSVC makes both sz1 & sz2 to be 4
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to save memory by using bit-precision in C++?

    Unless you're manipulating hundreds of millions of those things, it's probably not worth overthinking it.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to save memory by using bit-precision in C++?

    I don't even understand what bits have to do with this?

    Isn't an enum what you are looking for? Or you can put the integer value in an unsigned char, it doesn't get much smaller than byte...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How to save memory by using bit-precision in C++?

    I usually add #pragma pack to try and reduce memory, but again, it won't result in much (and it isn't cross compiler.) Bit fields in classes and structs might help. I assume you are already using -Os when compiling?

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: How to save memory by using bit-precision in C++?

    Quote Originally Posted by ertss View Post
    The first one returns 4 and the second even returns 8! I suppose it's in bytes, It just doesn't make sense.
    Instead of a bitset you can use a char. It's guaranteed by the C++ standard to be 8 bits wide. It will allow you to store 8 binary states. To set and reset those you can use the old C-style bit-fiddling idioms.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: How to save memory by using bit-precision in C++?

    Saving memory shouldn't be your only concern. What about speed of execution? Extracting bits from bytes or bytes from words can be more time consuming than storing values using the natural type of the processor. In my experience, speed is usually more important then memory but it does depend on your platform type. If you have plenty of memory, then you want to worry about optimizing for speed instead. That doesn't mean that you should be purposefully wasteful. This is why many projects have coding standards specific to the platform type that you are coding for.

    I've always found it amusing when people use 8 bit types within functions rather than just the natural types, because they think that they are saving memory.

  8. #8
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Question Re: How to save memory by using bit-precision in C++?

    Quote Originally Posted by nuzzle View Post
    Instead of a bitset you can use a char. It's guaranteed by the C++ standard to be 8 bits wide. It will allow you to store 8 binary states. To set and reset those you can use the old C-style bit-fiddling idioms.
    Is it? This is what the C++ standard says about it.
    Quote Originally Posted by C++ Std 3.9.1
    Objects declared as characters (char) shall be large enough to store any member of the implementation’s
    basic character set. If a character from this set is stored in a character object, the integral value of that character
    object is equal to the value of the single character literal form of that character. It is implementationdefined
    whether a char object can hold negative values.

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: How to save memory by using bit-precision in C++?

    Quote Originally Posted by kempofighter View Post
    Is it? This is what the C++ standard says about it.
    I was wrong. I thought I had read it somewhere. Sorry about that.

    Still my advice stands. The OP should be able to locate an 8 bit wide implementation dependent type supported by his compiler. It's very likely to exist.

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