CMultiBits Class

By | 2019-12-17

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. The Long Arrays are in place of Decimals but can be expanded way beyond Decimal size. It should be noted that the class can only handle one dimensional long arrays.)

Features

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.

Interface

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)

Attachments

FileUploadedSize
1071-20191217-063202-CMultiBits.zip12/17/2019 6:32:02 AM30154
Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.