Link to home
Start Free TrialLog in
Avatar of deeptish
deeptish

asked on

Bit Manipulation in VB5.0

How to do the Bit Manipulation in VB5.0 like Shift left or right ? ( eqv. in C  >> or << )
Avatar of waty
waty
Flag of Belgium image

' #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


ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mirkwood
Mirkwood

Bought This Question.