Module: BitOps in Category General VBA/VB6 : Data Type and Unit Conversion from Total Visual SourceBook

Routines for bitwise and unsigned data manipulation using VBA and VB6.

Visual Basic does not provide any native methods for performing bitwise manipulation, nor does it provide unsigned data types. This module offers routines for bit shifting, bit setting, clearing bits, unsigned assignment operations, and extracting components of variables.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modBitOps module.
BitClearByte Procedure Clear a bit in a byte variable.
BitClearInteger Procedure Clear a bit in an integer variable.
BitClearLong Procedure Clear a bit in a long variable.
BitSetByte Procedure Set a bit in a byte variable.
BitSetInteger Procedure Set a bit in an integer variable.
BitSetLong Procedure Set a bit in a long variable.
BitTestByte Procedure Test a bit in a byte to see if it is turned on.
BitTestInteger Procedure Test a bit in an integer to see if it is turned on.
BitTestLong Procedure Test a bit in a long integer to see if it is turned on.
dblToLong Procedure Perform an unsigned conversion from a double value to a long value. Handles doubles greater than 2,147,483,647 and less than or equal to 4,294,967,295.
HiByte Procedure Get the high byte of an integer.
HiLong Procedure Get the high long of a double.
HiWord Procedure Get the high integer of a long integer.
IntToByte Procedure Perform an unsigned conversion from an integer value to a byte value. This procedure correctly handles any integer value. For example, lngNumber = -1 assigns -1 to the variable lngNumber. However, lngNumber = IntToLong(-1) assigns 65,535 to lngNumber.
IntToLong Procedure Convert an integer value to a long value, treating the integer as unsigned. For example, performing lngNumber = -1, assigns -1 to the variable. However, performing lngNumber = IntToLong(-1) assigns 65,535.
LoByte Procedure Get the low byte of an integer.
LoLong Procedure Get the low long of a double.
LongToDbl Procedure This routine converts a long value to a double value, treating the long as unsigned. For example, performing dblNumber = -1, assigns -1 to the variable dblNumber. However, dblNumber = LongToDbl(-1), assigns 4,294,967,295.
LongToInt Procedure Perform an unsigned conversion from a long value to an integer value.
LoWord Procedure Get the low integer of the passed long.
MakeDbl Procedure Combine two longs into a double while handling negative values.
MakeInt Procedure Combine two bytes into an integer.
MakeLong Procedure Combine two words (integers) into a long while handling negative values.
Shlb Procedure Shift a byte value left by the specified number of bits. Left shifting is a multiplication operation. For the number of bits to shift to the left, raise two to that power, then multiply the result by the original value.
Shli Procedure Shift an integer value left by the specified number of bits. Left shifting is a multiplication operation. For the number of bits to shift to the left, raise two to that power, then multiply the result by the original value.
Shll Procedure Shift a long integer value left by the specified number of bits. Left shifting is a multiplication operation. For the number of bits to shift to the left, raise two to that power, then multiply the result by the original value.
Shrb Procedure Shift a byte value right the selected number of places. Right shifting is a division operation. For the number of bits to shift to the right, raise two to that power, then divide the original value by the result.
Shri Procedure Shift an integer value right the selected number of places. Right shifting is a division operation. For the number of bits to shift to the right, raise two to that power, then divide the original value by the result.
Shrl Procedure Shift a long integer value right the selected number of places. Right shifting can be defined as a division operation. For the number of bits to shift a value to the right, raise two to that power, then divide our original value by the result.
' Example of modBitOps
'
' To try this example, do the following:
' 1. Create a new form
' 2. Add a command button 'cmdTest'
' 3. Paste all the code from this example to the new form's module
' 4. Run the form

Private Sub cmdTest_Click()
  Dim lngHiLong As Long
  Dim lngLoLong As Long
  Dim intHiInt As Integer
  Dim intLoInt As Integer
  Dim bytHiByte As Byte
  Dim bytLoByte As Byte
  Dim lngValue As Long
  Dim intValue As Integer
  Dim bytValue As Byte

  Const cdblValue As Double = 1234567892343#
  Const clngValue As Long = 32
  Const cintValue As Long = 16
  Const cbytValue As Long = 8
  Const cintPlaces As Long = 4

  ' Shift left long
  Debug.Print clngValue & " shifted left " & cintPlaces & " places is: " & Shll(clngValue, cintPlaces)

  ' Shift right long
  Debug.Print clngValue & " shifted right " & cintPlaces & " places is: " & Shrl(clngValue, cintPlaces)

  ' Shift left integer
  Debug.Print cintValue & " shifted left " & cintPlaces & " places is: " & Shli(cintValue, cintPlaces)

  ' Shift right integer
  Debug.Print cintValue & " shifted right " & cintPlaces & " places is: " & Shri(cintValue, cintPlaces)

  ' Shift left byte
  Debug.Print cbytValue & " shifted left " & cintPlaces & " places is: " & Shlb(cbytValue, cintPlaces)

  ' Shift right byte
  Debug.Print cbytValue & " shifted right " & cintPlaces & " places is: " & Shrb(cbytValue, cintPlaces)

  ' Get longs from double
  lngHiLong = HiLong(cdblValue)
  lngLoLong = LoLong(cdblValue)
  Debug.Print "The hi long of " & cdblValue & " is " & lngHiLong & " and the lo long is " & lngLoLong

  ' Get integers from long
  intHiInt = HiWord(lngLoLong)
  intLoInt = LoWord(lngLoLong)
  Debug.Print "The hi integer of " & lngLoLong & " is " & intHiInt & " and the lo integer is " & intLoInt

  ' Get bytes from integer
  bytHiByte = HiByte(intHiInt)
  bytLoByte = LoByte(intHiInt)
  Debug.Print "The hi byte of " & intHiInt & " is " & bytHiByte & " and the lo byte is " & bytLoByte

  ' Reconstruct values
  Debug.Print "The integer with hi byte " & bytHiByte & " and lo byte " & bytLoByte & " is " & MakeInt(bytHiByte, bytLoByte)
  Debug.Print "The long with hi integer " & intHiInt & " and lo integer " & intLoInt & " is " & MakeLong(intHiInt, intLoInt)
  Debug.Print "The double with hi long " & lngHiLong & " and lo long " & lngLoLong & " is " & MakeDbl(lngHiLong, lngLoLong)

  ' Set bit long
  Debug.Print "Setting bit number " & cintPlaces
  BitSetLong lngValue, cintPlaces

  ' Test bit long
  Debug.Print "Testing bit number " & cintPlaces & " value is " & BitTestLong(lngValue, cintPlaces)

  ' Clear bit long
  Debug.Print "Clearing bit number " & cintPlaces
  BitClearLong lngValue, cintPlaces

  ' Set bit integer
  Debug.Print "Setting bit number " & cintPlaces
  BitSetInteger intValue, cintPlaces

  ' Test bit integer
  Debug.Print "Testing bit number " & cintPlaces & " value is " & BitTestInteger(intValue, cintPlaces)

  ' Clear bit integer
  Debug.Print "Clearing bit number " & cintPlaces
  BitClearInteger intValue, cintPlaces

  ' Set bit byte
  Debug.Print "Setting bit number " & cintPlaces
  BitSetByte bytValue, cintPlaces

  ' Test bit byte
  Debug.Print "Testing bit number " & cintPlaces & " value is " & BitTestByte(bytValue, cintPlaces)

  ' Clear bit byte
  Debug.Print "Clearing bit number " & cintPlaces
  BitClearByte bytValue, cintPlaces

  ' Convert an integer to a long
  Debug.Print "The integer -1 is the long value " & IntToLong(-1) & " when treated as unsigned"

  ' Convert a long to an integer
  Debug.Print "The long 50000 is the integer value " & LongToInt(50000)

  ' Convert a long to a double
  Debug.Print "The long -1 is the double value " & LongToDbl(-1) & " when treated as unsigned"

  ' Convert a double to a long
  Debug.Print "The double 2147483649 is the long value " & dblToLong(2147483649#)
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS