Solved

Bit Manipulation in VB5.0

Posted on 1999-01-28
3
285 Views
Last Modified: 2010-05-03
How to do the Bit Manipulation in VB5.0 like Shift left or right ? ( eqv. in C  >> or << )
0
Comment
Question by:deeptish
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Expert Comment

by:waty
ID: 1471128
' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 30/10/98
' * Time             : 15:51
' * Module Name      : Bitwise_Module
' * Module Filename  :
' **********************************************************************
' * Comments         : Implement Right and Left Shift operations for 32-bit Integers
' *
' *
' **********************************************************************

Option Explicit

'*----------------------------------------------------------*
'* Name       : vbShiftLeft                                 *
'*----------------------------------------------------------*
'* Purpose    : Shift 32-bit integer value left 'n' bits.   *
'*----------------------------------------------------------*
'* Parameters : Value  Required. Value to shift.            *
'*            : Count  Required. Number of bit positions to *
'*            :        shift value.                         *
'*----------------------------------------------------------*
'* Description: This function is equivalent to the 'C'      *
'*            : language construct '<<'.                    *
'*----------------------------------------------------------*
Public Function vbShiftLeft(ByVal Value As Long, Count As Integer) As Long

   Dim i As Integer

   vbShiftLeft = Value
   For i = 1 To Count
      vbShiftLeft = vbShiftLeft * 2
   Next

End Function

'*----------------------------------------------------------*
'* Name       : vbShiftRight                                *
'*----------------------------------------------------------*
'* Purpose    : Shift 32-bit integer value right 'n' bits.  *
'*----------------------------------------------------------*
'* Parameters : Value  Required. Value to shift.            *
'*            : Count  Required. Number of bit positions to *
'*            :        shift value.                         *
'*----------------------------------------------------------*
'* Description: This function is equivalent to the 'C'      *
'*            : language construct '>>'.                    *
'*----------------------------------------------------------*
Public Function vbShiftRight(ByVal Value As Long, Count As Integer) As Long

   Dim i As Integer
   vbShiftRight = Value
   For i = 1 To Count
      vbShiftRight = vbShiftRight \ 2
   Next
End Function

'*----------------------------------------------------------*
'* Name       : vbShiftLeftWord                             *
'*----------------------------------------------------------*
'* Purpose    : Shift 16-bit integer value left 'n' bits.   *
'*----------------------------------------------------------*
'* Parameters : Value  Required. Value to shift.            *
'*            : Count  Required. Number of bit positions to *
'*            :        shift value.                         *
'*----------------------------------------------------------*
'* Description: This function is equivalent to the 'C'      *
'*            : language construct '<<'.                    *
'*----------------------------------------------------------*
Public Function vbShiftLeftWord(ByVal Value As Long, Count As Integer) As Long

   Dim i As Integer
   ' Cut on 16-bit range
   vbShiftLeftWord = LOWORD(Value)
   For i = 1 To Count
      vbShiftLeftWord = vbShiftLeftWord * 2
   Next
   ' Cut on 16-bit range
   vbShiftLeftWord = LOWORD(vbShiftLeftWord)
End Function

'*----------------------------------------------------------*
'* Name       : vbShiftRightWord                            *
'*----------------------------------------------------------*
'* Purpose    : Shift 16-bit integer value right 'n' bits.  *
'*----------------------------------------------------------*
'* Parameters : Value  Required. Value to shift.            *
'*            : Count  Required. Number of bit positions to *
'*            :        shift value.                         *
'*----------------------------------------------------------*
'* Description: This function is equivalent to the 'C'      *
'*            : language construct '>>'.                    *
'*----------------------------------------------------------*
Public Function vbShiftRightWord(ByVal Value As Long, _
         Count As Integer) As Long
   Dim i As Integer

   ' Cut on 16-bit range
   vbShiftRightWord = LOWORD(Value)

   For i = 1 To Count
      vbShiftRightWord = vbShiftRightWord \ 2
   Next

End Function


0
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
ID: 1471129
'*----------------------------------------------------------*
'* Name       : BitSet                                      *
'*----------------------------------------------------------*
'* Purpose    : Sets a given Bit in Number                  *
'*----------------------------------------------------------*
Public Function BitSet(Number As Long, ByVal Bit As Long) As Long

   If Bit = 31 Then
      Number = &H80000000 Or Number
   Else
      Number = (2 ^ Bit) Or Number
   End If

   BitSet = Number

End Function

'*----------------------------------------------------------*
'* Name       : BitClear                                    *
'*----------------------------------------------------------*
'* Purpose    : Clears a given Bit in Number                *
'*----------------------------------------------------------*
Public Function BitClear(Number As Long, ByVal Bit As Long) As Long

   If Bit = 31 Then
      Number = &H7FFFFFFF And Number
   Else
      Number = ((2 ^ Bit) Xor &HFFFFFFFF) And Number
   End If

   BitClear = Number

End Function

'*----------------------------------------------------------*
'* Name       : BitIsSet                                    *
'*----------------------------------------------------------*
'* Purpose    : Test if bit 0 to bit 31 is set              *
'*----------------------------------------------------------*
Public Function BitIsSet(ByVal Number As Long, ByVal Bit As Long) As Boolean
   
   BitIsSet = False

   If Bit = 31 Then
      If Number And &H80000000 Then BitIsSet = True
   Else
      If Number And (2 ^ Bit) Then BitIsSet = True
   End If

End Function


0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1471130
Bought This Question.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question