-
May 21st, 2011, 12:28 AM
#1
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.
-
May 21st, 2011, 08:56 AM
#2
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
-
May 21st, 2011, 09:00 AM
#3
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.
-
May 23rd, 2011, 07:11 AM
#4
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.
-
May 23rd, 2011, 08:08 AM
#5
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?
-
May 23rd, 2011, 09:15 AM
#6
Re: How to save memory by using bit-precision in C++?
Originally Posted by ertss
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.
-
May 24th, 2011, 02:29 PM
#7
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.
-
May 24th, 2011, 02:32 PM
#8
Re: How to save memory by using bit-precision in C++?
Originally Posted by nuzzle
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.
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.
-
May 26th, 2011, 12:54 AM
#9
Re: How to save memory by using bit-precision in C++?
Originally Posted by kempofighter
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|