I want to reserve 1 bit for each element in an array. Since individual bits cannot be reserved, I have to reserve a byte instead. This is what I'm trying to do:
Array[57] = <1, 2, 3, ...>
For each element of "Array", I need 1 bit. I'm trying to compute the amount of bytes I will need to cover all of the elements. Note that the number 57 is subject to change. I can't seem to figure it out, because of this brain-fog of mine.
Here's an illustration: http://imagebin.org/229498
@TheIdeasMan: I've considered bit-fields, but they are not thread-safe, unfortunately. Also, I'll look into those examples to see if they match my needs :)
@Peter87: Yes, I've considered "std::vector<bool>", but again, it, too, is not suitable for my needs. I'm working with some restrictions, you see. Besides, the expression you posted might be what I'm looking for, but I need to know what "nBits is used for.
Edit: Thank you Peter87 :) I used your expression, modified it a little lit, and I got the result I needed. Greatly appreciated :) It turns out that I needed this:
int nBytes((nElementCount / CHAR_BIT) + (nElementCount % CHAR_BIT));
int nBytes((nElementCount / CHAR_BIT) + (nElementCount % CHAR_BIT));
That will overestimate the number of bytes needed. For instance when nElementCount is 7 it will calculate nBytes to 7. To store 7 bits you don't need 7 bytes! 1 byte is enough.
EDIT: I just realized that I made a mistake. The ternary operator should be inside the parentheses. int nBytes((nElementCount / CHAR_BIT) + (nElementCount % CHAR_BIT != 0 ? 1 : 0));