Question

In C# or VB.NET how I can retrieve the low-order DWORD of an Int64 value without Casting the value?

I've read this SO question but stills unclear to me, I work with VB.NET, anyways an answer says:

in many languages, all you need to do is cast it to an Int32. The top bits will be discarded.

This seems that works in C# just casting it to Integer:

((int)LongValue)

But in VB.NET seems not 'cause I've tried to cast it and I get a typicall arithmetic overflow exception.

I have a method like this to get a low order word of an Integer:

Public Shared Function GetLoWord(ByVal value As Integer) As Short

    If Value And &H8000I Then
        Return CShort(Value Or &HFFFF0000I)
    Else
        Return CShort(Value And &HFFFFI)
    End If

End Function

And now I would like to write the same method for a Long, to retrieve the low-DWORD, so how I can continue writting this?:

''' <summary>
''' Gets the low-order double word of an 'Int64' value.
''' </summary>
''' <param name="Value">Indicates the 'Int64' value that contains both the LoDword and the HiDword.</param>
''' <returns>The return value is the low-order double word.</returns>
Public Shared Function GetLoDword(ByVal value As Long) As Integer

    ' Code goes here...

End Function
Was it helpful?

Solution

Bit operations is what you need.

What you see in your code is masking. You can use a mask and the "and" operator to get the assigned bits within a range. So for the low-order dword, you need a mask with a value which represents all bits for a dword. Which is 2^32, or simply 0xFFFFFFFF.

If you needed the high order dword, you's use the shift operators.

For signed values, you need to take into consideration the highest order bit, it is 1 if it's a negative value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top