Introduction
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.
CXBitArray Features
Here are some of the methods provided by CXBitArray
:
- Constructor #1 - Construct un-initialized
CXBitArray
object. Before using the object, Init()
or 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 Attach()
.
- 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 LPBYTE()
operator.
- Constructor #4 - Construct
CXBitArray
object from file.
- Constructor #5 - Construct
CXBitArray
object from registry.
- Attach() - Attach an existing bit array to a
CXBitArray
object.
- 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
METHOD |
DESCRIPTION |
CXBitArray() #1 |
Construct un-initialized CXBitArray object |
CXBitArray() #2 |
Construct CXBitArray object from existing array |
CXBitArray() #3 |
Construct CXBitArray object with array allocation |
CXBitArray() #4 |
Construct CXBitArray object from file |
CXBitArray() #5 |
Construct CXBitArray object from registry |
~CXBitArray() |
Save bit array to file and/or registry and deallocate bit array |
Attach() |
Attach an existing bit array to a CXBitArray object |
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 |
Get() |
Get bit value |
GetArraySizeBits() |
Get size of bit array in bits |
GetArraySizeBytes() |
Get size of bit array in bytes |
GetBitNo() |
Convert bit mask to bit number |
GetBitPos() |
Convert bit number to byte and bit indexes and bit mask |
GetPersistFileName() |
Get persist file name |
GetRegistryKeyName() |
Get registry key name |
GetRegistryValueName() |
Get registry value name |
Init() |
Initialize CXBitArray object |
operator [] |
Get bit value at a bit index |
operator LPBYTE |
Get address of bit array |
ReadPersistFile() |
Read bit array from file |
ReadRegistry() |
Read bit array from registry |
Set() |
Set bit value |
SetAll() |
Set all bits to a value |
SetPersistFileName() |
Set persist file name |
SetRegistryNames() |
Set registry key and value names |
ToString() |
Returns a string that represents the bit array |
WritePersistFile() |
Write bit array to file |
WriteRegistry() |
Write bit array to registry |
Free and Used Bits
In 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
To integrate 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 CXBitArray
object:
- Construct uninitialized CXBitArray object -
Init()
or Attach()
must be called prior to using the object. CXBitArray();
- 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.
CXBitArray(80, 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);
Demo App
The XBitArrayTest.exe demo tests the methods in CXBitArray
:
Links
Revision History
Version 1.2 - 2004 February 10
Usage
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.