Library code snippets

Hi-words and Lo-words

When using API, we often need to place values in the hi word or lo word, and retreive these again. The code below provides a few useful functions for doing this

'creates a long variable out of two words
Private Function MakeLong(nLoWord As Integer, nHiWord As Integer) As Long
    'places two integer values into the hiword and loword
    MakeLong = nHiWord * 65536 + nLoWord
End Function

'extracts the hiword and loword from a long variable
Private Function GetWords(ByVal lParam As Long, ByRef lLoWord As Long, ByRef lHiWord As Long) As Boolean
    ' This is the LOWORD of the lParam:
    lLoWord = lParam And &HFFFF&
    ' lLoWord now equals 65,535 or &HFFFF
    ' This is the lHiWord of the lParam:
    lHiWord = lParam &H10000 And &HFFFF&
    ' HIWORD now equals 30,583 or &H7777
    GetWords = True = 1
End Function

Alternatively, you can use these two functions to extract the hiword or loword, submitted by Mike J

Function GetHiWord(dw As Long) As Integer
If dw& And &H80000000 Then
   GetHiWord% = (dw& \ 65535) - 1
Else
   GetHiWord% = dw& \ 65535
End If
End Function

Function GetLoWord(dw As Long) As Integer
If dw& And &H8000& Then
   GetLoWord% = &H8000 Or (dw& And &H7FFF&)
Else
   GetLoWord% = dw& And &HFFFF&
End If
End Function

Comments

  1. 19 Oct 2001 at 00:45

    I knew there was an explanation to this.. if it was simple or not was not to be said by this but it sure is cause for some reading. You may even have given me a hint to a solution of another problem I had, due to why I scrapped that thing.


    I might take it up again with this knowledge.


    Thanks.

  2. 18 Oct 2001 at 19:32

    From MSDN:


    For some API functions, such as SendMessage or PostMessage, you may need to package two short Integer values into a Long variable to pass them as a single parameter. This article demonstrates how to package such Integers and how to unpack them if necessary.


    MORE INFORMATION
    The trick to packing values is bit shifting. Because Visual Basic does not provide bit shift operators to use, you need to do things the old fashioned way; through multiplication. To make an Integer the high word for a Long value, you need to multiply it by &H10000. This has the effect of shifting the bit values 16-bits (2-bytes) to the left, making room for the low word value you want to add.


    Before you can add the low word value, however, you need to make an adjustment. Remember that Visual Basic Integer types are signed values, but the low word value needs to be unsigned if you plan to add it to your high word value. To make sure Visual Basic treats the low word as an unsigned integer, you need to perform a bitwise "And" on the value using &HFFFF& as a mask. In effect, this saves the value as a Long integer with the high (signed) bit cleared but keeps the original Integer's bit value preserved.

  3. 18 Oct 2001 at 19:13

    in Hi-Word and Lo-Word... cool... hmmmm what is Hi and Lo words? *smiles

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Hi-words and Lo-words.

Leave a comment

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