LoWord HiWord Vb.net Syntax

Unhnd_Exception 2 Tallied Votes 946 Views Share

Hopefully someone searches this one day and it becomes useful. I never found anything.

Below is the vb syntax for combining numbers in the same memory address.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim LngWord As Long = LngMakeWord(Integer.MaxValue, 65489)
    Dim LoLng As Integer = LngLoWord(LngWord)
    Dim HiLng As Integer = LngHiWord(LngWord)

    Dim IntWord As Integer = IntMakeWord(6536, 25548)
    Dim IntLo As Short = IntLoWord(IntWord)
    Dim IntHi As Short = IntHiWord(IntWord)

End Sub

Private Function IntMakeWord(ByVal loWord As Short, ByVal hiWord As Short) As Integer

    Return (CInt(hiWord) << 16) Or (CInt(loWord) And Short.MaxValue)

End Function

Private Function IntLoWord(ByVal word As Integer) As Short

    Return CShort(word And Short.MaxValue)

End Function

Private Function IntHiWord(ByVal word As Integer) As Short

    Return CShort((word >> 16))

End Function




Private Function LngMakeWord(ByVal loWord As Integer, ByVal hiWord As Integer) As Long

    Return CLng(hiWord) << 32 Or (CLng(loWord) And UInteger.MaxValue)

End Function

Private Function LngLoWord(ByVal word As Long) As Integer

    Return CInt(word And UInteger.MaxValue)

End Function

Private Function LngHiWord(ByVal word As Long) As Integer

    Return CInt(word >> 32)

End Function
zinnqu commented: Awsome as usual +2
zinnqu 7 Junior Poster in Training

Useful Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.