-1

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
Community
  • 1
  • 1
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • http://msdn.microsoft.com/en-us/library/aa711649(v=vs.71).aspx – siride Mar 09 '14 at 01:31
  • @siride thanks but what is supposed that I need to understand from that url? – ElektroStudios Mar 09 '14 at 01:35
  • 1
    you use the same logic for your `GetLoWord` as you do for `GetLoDWord`. I wasn't sure what, exactly, you were having trouble with, but the only thing that seemed obvious to me was that maybe you weren't sure how to express a 64-bit hex literal. – siride Mar 09 '14 at 01:40
  • the reason of the downvote is 'cause i'm not a math guru or what? – ElektroStudios Mar 09 '14 at 01:40
  • @siride the problem that I'm facing is that the LoWord method was not mine, I know the usage of integer literals but I don't know those (resulting) Int32 values exactlly what means, I'm just trying to make one method to work with LoDwords 'cause I need it to retrieve the lowDword of some API functions, but, it's hard for me. – ElektroStudios Mar 09 '14 at 01:52

1 Answers1

1

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.

JonPall
  • 814
  • 4
  • 9
  • Sorry but could you give a code example?... With this number `value as Long = 90000000000L` I've tried the masking that you suggested me: `Return CInt(value And &HFFFFFFFFL)` but I still get an arithmetic overflow exception. – ElektroStudios Mar 09 '14 at 01:40
  • @ElektroStudios: `Integer` is signed, so if you try to put something with a 1 in the uppermost of the 32 low bits, you will get an overflow. Use unsigned int instead (System.UInt32). – siride Mar 09 '14 at 01:41
  • Sorry but I don't understand what I need to do, I expect an integer as a returned datatype, but if I return an Unsigned integer I can't assign it to an integer value – ElektroStudios Mar 09 '14 at 01:50
  • @ElektroStudios: you have to make a decision. An `Integer`, being signed, can't hold the low 32 bits of a 64-bit integer. You can either take the low 31 bits, or you can use an unsigned integer. Or, you can the 32nd bit being 1 and make your result negative. Which you choose depends on your use cases. – siride Mar 09 '14 at 01:52
  • `being signed, can't hold the low 32 bits of a 64-bit integer` But, this casting in `C#` does not holds the 32 bits that you are talking about, in an signed integer? `((int)LongValue)` I'm confused 'cause that – ElektroStudios Mar 09 '14 at 01:54
  • 1
    @ElektroStudios: this answers that question: http://stackoverflow.com/questions/12713562/integer-overflow-converting-c-sharp-to-vb-net-code. – siride Mar 09 '14 at 02:02
  • @siride many thanks! I also discovered there an elegant solution for my problem using BitConverter Class, really thanks you and JonPall for your time explaining me. – ElektroStudios Mar 09 '14 at 02:17