The new vbAccelerator Site - more VB and .NET Code and Controls

HiWords and LoWords from Long Values

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

Bit Manipulation

Updated:

21/02/99

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


When translating C code to VB, you quite often come across the HiWord and LoWord operators, used to pack two integers into a long value. A simple translation of HiWord code will run into difficulties when unsigned integer arithmetic is being used in the C code and the highest bit of the long value can be set. Since VB doesn't support unsigned arithmetic we have to strip out the high bit and add it back again later to avoid overflows and misleading results.

Start a new project then add a module. Add the following code to the module:

Public Property Get LoWord(ByRef lThis As Long) As Long
LoWord = (lThis And &HFFFF&)
End Property

Public Property Let LoWord(ByRef lThis As Long, ByVal lLoWord As Long)
lThis = lThis And Not &HFFFF& Or lLoWord
End Property

Public Property Get HiWord(ByRef lThis As Long) As Long
If (lThis And &H80000000) = &H80000000 Then
HiWord = ((lThis And &H7FFF0000) \ &H10000) Or &H8000&
Else
HiWord = (lThis And &HFFFF0000) \ &H10000
End If
End Property

Public Property Let HiWord(ByRef lThis As Long, ByVal lHiWord As Long)
If (lHiWord And &H8000&) = &H8000& Then
lThis = lThis And Not &HFFFF0000 Or ((lHiWord And &H7FFF&) * &H10000) Or &H80000000
Else
lThis = lThis And Not &HFFFF0000 Or (lHiWord * &H10000)
End If
End Property




&nbsp

Related Tips and Articles:

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 21/02/99