Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bit Manipulation in VB5.0

Posted on 1999-01-28
3
Medium Priority
?
302 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

688 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