Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB7
Expertise: Intermediate
Oct 28, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Retrieving the hi/low byte/word of a value, and other operations

As explained in the tip about the ARGBColor structure (look at the end of this tip for the link), we can define a structure and have its fields that point to the same memory address, but that read a different number of bytes. This makes easier to define a structure that allows us to set a value to a field, and then retrieve the single bytes/words of that value with the other fields. Here's how we can define this structure:

<StructLayout(LayoutKind.Explicit)> Structure IntegerTypes
    ' A 64-bit integer
    <FieldOffset(0)> Dim Long0 As Long
    ' Two 32-bit integers
    <FieldOffset(0)> Dim Integer0 As Integer
    <FieldOffset(4)> Dim Integer1 As Integer
    ' Four 16-bit integers
    <FieldOffset(0)> Dim Short0 As Short
    <FieldOffset(2)> Dim Short1 As Short
    <FieldOffset(4)> Dim Short2 As Short
    <FieldOffset(6)> Dim Short3 As Short
    ' Eight 8-bit integers
    <FieldOffset(0)> Dim Byte0 As Byte
    <FieldOffset(1)> Dim Byte1 As Byte
    <FieldOffset(2)> Dim Byte2 As Byte
    <FieldOffset(3)> Dim Byte3 As Byte
    <FieldOffset(4)> Dim Byte4 As Byte
    <FieldOffset(5)> Dim Byte5 As Byte
    <FieldOffset(6)> Dim Byte6 As Byte
    <FieldOffset(7)> Dim Byte7 As Byte

    ' Low byte of a word
    Function LowByte(ByVal Value As Long) As Byte
        Long0 = Value
        Return Byte0
    End Function

    ' High byte of a word
    Function HighByte(ByVal Value As Long) As Byte
        Long0 = Value
        Return Byte1
    End Function

    ' Low word of a doubleword
    Function LowWord(ByVal Value As Long) As Short
        Long0 = Value
        Return Short0
    End Function

    ' High word of a doubleword
    Function HighWord(ByVal Value As Long) As Short
        Long0 = Value
        Return Short1
    End Function

End Structure
Here's how you can test the IntegerTypes structure to retrieve the hi/low byte/word of a value, and to do other operations:

Dim it As IntegerTypes

it.Short0 = 517                  ' hex 0205
Console.WriteLine(it.Byte0)        ' => 5
Console.WriteLine(it.Byte1)        ' => 2

Console.WriteLine(it.LowByte(517))           ' => 5
Console.WriteLine(it.HighByte(517))          ' => 2
Console.WriteLine(it.LowWord(&HFFFF1000))    ' => 4096
Console.WriteLine(it.HighWord(&HFFFF1000))   ' => -1
Note: this code is taken from Francesco Balena's Programming Microsoft Visual Basic .NET book (MS Press 2002)
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date