CXBitArray encapsulates a class for handling a bit array. Bit arrays are used most often to represent a collection of objects - for example, blocks of memory in a memory pool. Each bit in the array corresponds to an object, and is referenced by a "bit number", where bit number 0 is the first bit.
The underlying data structure of
CXBitArray is assumed to be an array of bytes, which can either be allocated by
CXBitArray or can be passed as an address to the
CXBitArray constructor. For example, this code:
CXBitArray bit(80, 0);
would construct a
CXBitArray object, which would then allocate an array of 10 bytes (80 / 8) that are initialized to 0. Bit number 79 would be the last bit.
Here are some of the methods provided by
- Constructor #1 - Construct un-initialized
CXBitArray object. Before using the object,
Attach() must be called.
- Constructor #2 - Construct
CXBitArray object from existing array. This constructor will initialize the bit array. To use an existing array without initializing it, use
- Constructor #3 - Construct
CXBitArray object with array allocation. The bit array will be allocated from the heap. To get the address of the bit array, use the
- Constructor #4 - Construct
CXBitArray object from file.
- Constructor #5 - Construct
CXBitArray object from registry.
- Attach() - Attach an existing bit array to a
- Count() - Return counts of bits set to 0 and bits set to 1.
- Find() - Find next bit that has the value specified by
bit_value. This method can be used to manage any kind of circular list or buffer pool, where the items are continuously being allocated in front of the index and consumed behind the index.
Find() will scan all bits in the bit array (wrapping the search to the beginning if necessary) until a bit with the specified value is found.
- Get() - Get bit value.
- Set() - Set bit value.
- SetAll() - Set all bits to a value.
- ToString() - Returns a pointer to a string that represents the bit array. The string must be deallocated by the caller.
CXBitArray Class Reference
|Construct un-initialized |
CXBitArray object from existing array
CXBitArray object with array allocation
CXBitArray object from file
CXBitArray object from registry
|Save bit array to file and/or registry and deallocate bit array|
|Attach an existing bit array to a |
|Return counts of bits set to 0 and bits set to 1|
|Find next bit that has the value specified by |
|Get bit value|
|Get size of bit array in bits|
|Get size of bit array in bytes|
|Convert bit mask to bit number|
|Convert bit number to byte and bit indexes and bit mask|
|Get persist file name|
|Get registry key name|
|Get registry value name|
|Get bit value at a bit index|
|Get address of bit array|
|Read bit array from file|
|Read bit array from registry|
|Set bit value|
|Set all bits to a value|
|Set persist file name|
|Set registry key and value names|
|Returns a string that represents the bit array|
|Write bit array to file|
|Write bit array to registry|
Free and Used Bits
CXBitArray, there is no built-in assumption about what constitutes a "free" or a "used" bit. The concepts of "free" and "used" are determined by the application. For example, a backup utility might consider a "used" bit to mean "archived", and a "free" bit to mean "not archived". Or an appointment application might consider a "used" bit to mean "not available", and a "free" bit to mean "available". It does not matter to
CXBitArray whether a "free" bit is defined to have a value of 0 or 1.
CXBitArray has methods to set, test, and find bits with values of 0 or 1.
How To Use
CXBitArray class into your app, you first need to add the following file to your project:
For details on how to use
CXBitArray object, refer to the code in XBitArrayTestDlg.cpp.
Here are some examples of how to construct a
- Construct uninitialized CXBitArray object -
Attach() must be called prior to using the object.
- Construct CXBitArray object from existing array - the address specified by
bitarray will be used to access the 80-bit bit array, which will be initialized to 0.
CXBitArray(bitarray, 80, 0);
- Construct CXBitArray object with array allocation - an 80-bit bit array will be allocated from the heap and initialized to 0.
- Construct CXBitArray object from file - the bit array (allocated from the heap, according to the size of the file) will be read from the file bitarray.dat. If the bit array cannot be read from the file, an 80-bit bit array will be allocated from the heap and initialized to 1.
CXBitArray("bitarray.dat", 80, 1);
- Construct CXBitArray object from registry - the bit array (allocated from the heap, according to the size of the registry value) will be read from the registry value at Software\CodeProject\XBitArrayTest\BitArray. If the bit array cannot be read from the registry, an 80-bit bit array will be allocated from the heap and initialized to 1.
CXBitArray("Software\\CodeProject\\XBitArrayTest", "BitArray", 80, 1);
The XBitArrayTest.exe demo tests the methods in
Version 1.2 - 2004 February 10
This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.