short val1 = short.MaxValue;
short val2 = short.MaxValue;
int result = val1;
result |= val2 << 16;
Console.WriteLine( "Result =\t" + result ); //2147450879
Console.WriteLine( "Expected =\t" + int.MaxValue ); //2147483647
short val1 = short.MaxValue; short val2 = short.MaxValue; int result = val1; result |=
Share
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):