winzip icon


Submitted on: 1/8/2017 2:08:23 AM
By: Robert Dee 
Level: Advanced
User Rating: By 6 Users
Compatibility: VB 5.0, VB 6.0
Views: 16495

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.

winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

8/13/2016 10:05:33 PMRobert Dee

Question for those reading here: The CMultibits class already uses Currencies (64 bit). Would there be any value/use in adding the ability to handle the Double type (also 64 bit)?
(If this comment was disrespectful, please report it.)


Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.