How to make two WORD to DWORD
short val1 = short.MaxValue;
short val2 = s开发者_如何转开发hort.MaxValue;
int result = val1;
result |= val2 << 16;
Console.WriteLine( "Result =\t" + result ); //2147450879
Console.WriteLine( "Expected =\t" + int.MaxValue ); //2147483647
short
is signed, so the maximum value is 0x7FFF
. Concatenated this results in 0x7fff7fff
instead of 0x7fffffff
which is 2147450879. So what you're seeing is actually correct.
That looks like C#. Short is signed. A signed negative value extended to int will fill all the leftmost 16 bits. Thus, the proposed code will fail whenever "val1" is negative.
This code works (note that WORD and DWORD are unsigned quantities):
public static uint MakeDWord(ushort a, ushort b) {
return ((uint)a << 16) | b;
}
This is what you need
ushort val1 = ushort.MaxValue;
ushort val2 = ushort.MaxValue;
int result = val1;
result |= val2 << 15;
try this, similar to MAKEWORD in < windef.h >:
#define MAKEDWORD(_a, _b) ((DWORD)(((WORD)(((DWORD_PTR)(_a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(_b)) & 0xffff))) << 16))
精彩评论