masking and shifting in VB

vb6 Grenada
  • 14 years ago

    i want to know about bit masking and shifting operations in VB
    i know it in C but how to do the foleeing c operation in VB

    i want to extract 90 
    a=  (&H90820002 & &HFF0000) >> 24

  • 14 years ago
    Harish,

    You will have to give a better description of what you are trying to do!!!  Just saying "I want to extract 90" doesn't tell us much.  Do you want to extract characters in a certain position?  In this case, the characters at positions 7 and 8? or do you want to extract the two characters after the characters a= (&H?

    This example will demonstrate how to use InStr, Mid and Left to extract the two characters after the first &H in the given string:
    Private Sub Command1_Click()
    Dim strInput As String
    Dim strOutput As String
    strInput = "a=  (&H90820002 & &HFF0000) >> 24"
    If InStr(1, strInput, "&H") Then
        strOutput = Mid(strInput, InStr(1, strInput, "&H") + 2)
        strOutput = Left(strOutput, 2)
    End If
    Label1.Caption = strOutput
    End Sub

    This works by first using the "InStr" function (which searches a string for another string and returns the position at which that string starts) to return the start of the string "&H".  It then chops off everything before this string and the string itself using the "Mid"  function(which returns everything to the right of a given point - in this case the start position of the string &H + 2 chars for the &H itself).  So the line:
    strOutput = Mid(strInput, InStr(1, strInput, "&H") + 2)
    Will start with the string:
    a=  (&H90820002 & &HFF0000) >> 24
    and chop off the beginning to give:
    90820002 & &HFF0000) >> 24
    It then uses the "Left" function which works like the Mid function but will return everything to the left of a given point.  In this case:
    strOutput = Left(strOutput, 2)
    will return the first two characters of our string:
    90

    I apologise if this is not what you require but you will never get the desired reply if you post obscure questions!!  


























  • 14 years ago
    harish139 wrote:

    i want to know about bit masking and shifting operations in VB
    i know it in C but how to do the foleeing c operation in VB

    i want to extract 90 
    a=  (&H90820002 & &HFF0000) >> 24


    Usually there is no need for high level computer languages to support operations like bit masking or shifting of bit masks. These operations are low level operations and when a computer language has a set of commands of them, they usually are compiled 1:1 into assembler code.
    High level computer languages deliver a set of constants that represents the results of bit masks. Just have a look at the MouseMove event, especially on 'button' and 'shift'.
    If you still need bit masking and shifting (e.g. some graphical tasks), you can use the basic logic operations: AND, OR, XOR.


  • 14 years ago

    You can use mathmaticaly formula for shifting (since vb does not support any bit shifting operator)

    First we should understand how shifting works

    Right Shifting will always decrease number while Left Shifting will always increase number

    E.g.

    1001 >> 1  = 100               (1 bit removed from right)

    1001 << 1 = 10010            (1 bit added at right)

    Therefore:

    RightShift = number / 2 ^ bits      (number >> bits)

    LeftShift = number * 2 ^ bits       (number << bits)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”