Solved

Bit Manipulation in VB5.0

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 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