How to Mimic HIWORD, LOWORD, HIBYTE, LOBYTE C Macros in VB

Last reviewed: June 21, 1995
Article ID: Q112651
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic

  programming system for Windows, versions 2.0 and 3.0

SUMMARY

Visual Basic does not provide any bitwise functions for pulling apart numeric values. In C, there are macros (HIBYTE, LOBYTE, HIWORD, and LOWORD) to separate parts of long integers into two parts, or separate integers into two parts. This article shows by example how to do the same thing in a Visual Basic program.

MORE INFORMATION

The HIBYTE, LOBYTE, HIWORD, and LOWORD macros are defined in C in the WINDOWS.H file. The HIBYTE and LOBYTE macros are used to retrieve the high-order or low-order byte of the integer passed to them. The HIWORD and LOWORD macros retrieve the high-order or low-order word from a long integer value passed to them.

Step-by-Step Example

This example uses Visual Basic to mimic the HIBYTE, LOBYTE, HIWORD, and LOWORD macros

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add two command buttons (Command1 and Command2) to Form1.

  3. Add the following code to the Comamnd1_Click event:

       Sub Command1_Click ()
          Dim wParam As Integer
          Dim LOBYTE As Integer
          Dim HIBYTE As Integer
          ' Set wParam to a value:
          wParam = &H77FF
          ' Make call to function:
          ret = gethilobyte(wParam, LOBYTE, HIBYTE)
          ' Print out return values:
          Print LOBYTE, HIBYTE
       End Sub
    
    

  4. Add the following code to the Comamnd2_Click event:

       Sub Command2_Click ()
          Dim lParam As Long
          Dim LOWORD As Long
          Dim HIWORD As Long
          ' Set lParam to a value:
          lParam = &H7777FFFF
          ' Make call to function:
          ret = gethiloword(lParam, LOWORD, HIWORD)
          ' Print out return values:
          Print LOWORD, HIWORD
       End Sub
    
    

  5. Add the following code to the general declarations section of Form1:

       ' Enter the following Function statement as one, single line:
       Function gethilobyte(wparam as integer, LOBYTE as integer,
          HIBYTE as integer)
          ' This is the LOBYTE of the wParam:
          LOBYTE = wParam And  &HFF&
          ' This is the HIBYTE of the wParam:
          HIBYTE = wParam  \  &H100  And  &HFF&
          gethilobyte = 1
       End Function
    
    

  6. Add the following code to the general declarations section of Form1:

    Function gethiloword(lparam as long, LOWORD as long, HIWORD as long)

          ' This is the LOWORD of the lParam:
          LOWORD = lParam And  &HFFFF&
          ' LOWORD now equals 65,535 or &HFFFF
          ' This is the HIWORD of the lParam:
          HIWORD = lParam  \  &H10000  And  &HFFFF&
          ' HIWORD now equals 30,583 or &H7777
          gethiloword = 1
    
    End Function

  7. Run the program. Click the Command1 button to pull apart an integer value into its high-order and low-order bytes. Click the Command2 button to pull apart a long integer into its high-order and low-order words.


Additional reference words: 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.