Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use StructLayout in VB.NET:</p> <p><em>correction: word is 16bit, dword is 32bit</em></p> <pre><code>&lt;StructLayout(LayoutKind.Explicit, Size:=4)&gt; _ Public Structure UDWord &lt;FieldOffset(0)&gt; Public Value As UInt32 &lt;FieldOffset(0)&gt; Public High As UInt16 &lt;FieldOffset(2)&gt; Public Low As UInt16 Public Sub New(ByVal value As UInt32) Me.Value = value End Sub Public Sub New(ByVal high as UInt16, ByVal low as UInt16) Me.High = high Me.Low = low End Sub End Structure </code></pre> <p>Signed would be the same just using those types instead</p> <pre><code>&lt;StructLayout(LayoutKind.Explicit, Size:=4)&gt; _ Public Structure DWord &lt;FieldOffset(0)&gt; Public Value As Int32 &lt;FieldOffset(0)&gt; Public High As Int16 &lt;FieldOffset(2)&gt; Public Low As Int16 Public Sub New(ByVal value As Int32) Me.Value = value End Sub Public Sub New(ByVal high as Int16, ByVal low as Int16) Me.High = high Me.Low = low End Sub End Structure </code></pre> <p><strong>EDIT:</strong></p> <p>I've kind of rushed the few times I've posted/edited my anwser, and yet to explain this solution, so I feel I have not completed my answer. So I'm going to do so now:</p> <p>Using the StructLayout as explicit onto a structure requires you to provide the positioning of each field (by byte offset) <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx" rel="nofollow noreferrer" title="MSDN Documentation">[StructLayoutAttribute]</a> with the FieldOffset attribute <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.fieldoffsetattribute.aspx" rel="nofollow noreferrer" title="MSDN Documentation">[FieldOffsetAttribute]</a></p> <p>With these two attributes in use you can create overlapping fields, aka <a href="http://en.wikipedia.org/wiki/Union_(computer_science)" rel="nofollow noreferrer" title="Wikipedia - Union (computer science)">unions</a>.</p> <p>The first field (DWord.Value) would be the 32bit integer, with an offset of 0 (zero). To split this 32bit integer you would have two additional fields starting again at the offset of 0 (zero) then the second field 2 more bytes off, because a 16bit (short) integer is 2 bytes a-peice.</p> <p>From what I recall, usually when you split an integer they normally call the first half "high" then the second half "low"; thus naming my two other fields.</p> <p>With using a structure like this, you could then create overloads for operators and type widing/narrowing, to easily exchange from say an Int32 type to this DWord structure, aswell as comparasions <a href="http://msdn.microsoft.com/en-us/library/ms379613(VS.80).aspx" rel="nofollow noreferrer" title="MSDN VB Operator Overloading">Operator Overloading in VB.NET</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload