|
-
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.
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
|