Q:

C++: I need some guidance in how to create dynamic sized bitmaps

I'm trying to create a simple DBMS and although I've read a lot about it and have already designed the system, I have some issues about the implementation.
I need to know what's the best method in C++ to use a series of bits whose length will be dynamic. This series of bits will be saved in order to figure out which pages in the files are free and not free. For a single file the number of pages used will be fixed, so I can probably use a bitset for that. However the number of records per page AND file will not be fixed. So I don't think bitset would be the best way to do this.
I thought maybe to just use a sequence of characters, since each character is 1 byte = 8 bits maybe if I use an array of them I would be able to create the bit map that I want.
I never had to manipulate bits at such a low level, so I don't really know if there is some other better method to do this, or even if this method would work at all.
thanks in advance

A:

If you are just wanting the basics on the bit twiddling, the following is one way of doing it using an array of characters.  
Assume you have an array for the bits (the length needs to be (totalitems / 8 )):
unsigned char *bits;  // this of course needs to be allocated somewhere

You can compute the index into the array and the specific bit within that position as follows:
// compute array position
int pos = item / 8;  // 8 bits per byte
// compute the bit within the byte.  Could use "item & 7" for the same
// result, however modern compilers will typically already make
// that optimization.
int bit = item % 8;

And then you can check if a bit is set with the following (assumes zero-based indexing):
if ( bits[pos] & ( 1 << bit ))
  return 1;  // it is set
else
  return 0;  // it is not set

The following will set a specific bit:
bits[pos] |= ( 1 << bit );

And the following can be used to clear a specific bit:
bits[pos] &= ~( 1 << bit );

