CMultiBits [Updated January 7, 2017]
CMultiBits is a class, with no dependencies, that handles
bit manipulation in Byte, Integer, Long, Single, Double, Currency, Dates, and
Long Arrays. (These are VarTypes 2 thru 7, 17 and 8195.)
CMultiBits manipulates bits (including the sign bit) in all the above
data types. It allows creating bitfields of any size from 8 bits (Byte)
to 64 bits (Currency, Dates or Doubles), or up to unlimited size using
a long array acting as a single bit field. (You can even do bit shifting and rotating on a long array and it will act as if it were a single bitfield.) And all of these variables (including long arrays) all use the same public interface.
Another feature is the ability to store numeric values within
a bitfield, right along with boolean values, making for what
might be called a 'hybrid bitfield.'
The class features the option to use typecasting for strings, Singles,
64 bit data types and long arrays. It also makes use of VB's hidden GetMem
and PutMem methods. (Typecasting was adapted from a Bonnie West
class - many thanks.) There are also a number of optional parameters
that allow some flexibility in the class.
The class also has a complete collection of Byte/Integer/Long
/Currency Combo & Extraction routines, as well has bit/hex
string read & write routines.
Copious notes are included to help understand the code.
For bit manipulation: SetBitFlag/ClearBitFlag (single & multiple bits),
Toggle, Shift, Rotate, ReverseBits, ReverseBytes, CountBits,
Bit/Hex string creating & reading. CustomValue (property
get/let) is for inserting & extracting numeric values from bit
fields. IsFlagged reads one bit or multiple bits.
The combo/extract routines include:
LoNibble/HiNibble/MakeByte (Extract/combine nibbles.)
LoByte/HiByte/MakeWord (Extract/combine bytes.)
LoWord/HiWord/MakeDWord (Extract/combine integers.)
LoDWord/HiDWord/MakeQWord (Extract/combine longs.)
LongFromBytes (Combine 1 to 4 bytes to form a long.)
ByteFromLong (Extract any byte from a long.)
ByteIntoLong (Insert a byte into any position of an
existing long.) ByteFromCurr (Extract any byte from currency) ByteIntoCurr (Insert byte into any byte position of currency. IntFromCurr (Extract any integer from a currency), IntIntoCurr (Insert an integer into any int position of a currency)
1/7/17: The changes are in the LONG_ARRAY case of HexString and ReadHexString. Added the ability to make and read a single hex string for long arrays. Previously the routines would only make a comma delimited string with the comma separating each element. Now HexString will optionally make either style. And ReadHexString will automatically detect and read either style. See the comments at each procedure and the "Use64BitHex" property for details. There are no changes to the public interface. Any improvements, ideas or bug reports would be much appreciated. See just below "Option Explicit" for a complete listing of features and improvement history.