The problem isn't with the code (although there may be more efficient ways to do split integers). By attempting to represent an int
as two signed 16 bit shorts, you now need to consider that the sign bit could be present in both shorts
. Hence the comment that ushort[]
would be a more appropriate choice of representation of the two 16 bit values.
The problem seems to be with the understanding of why a 4 byte signed integer (DWORD) can't be effectively represented in two 2 byte signed shorts (WORD)s.
The problem is that -25814 is 0xFFFF 9B2A
This isn't true - you've represented -25814
as a short
, so it can't possibly be 0xFFFF 9B2A
- that's a 32 bit representation. Its 16 bit representation is just 9B2A
.
If you open up the Calculator on Windows, and set the mode to programmer, and tinker with the HEX
and DEC
bases, and then flipping between DWORD
and WORD
representations of the values, you should see that the 16 bit values you've extracted from the 32 bit int are correctly represented (provided that you understand the representation):
Your original 32 bit Integer (DWORD) is 1851628330:

The high word 28253 doesn't have the sign bit set, so you seem satisfied with the conversion to 6E5D:

However, if the low word is interpreted as a signed short, then you'll find that it's bit sign is set, so hence it reported as a negative value. However, the representation (bits, and hex) does correctly represent the last 16 bits of your original 32 bit int.
