Solved

Bit Manipulation in VB5.0

Posted on 1999-01-28
3
242 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
  • 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now