My understanding is that conversion of WORD type data converts to DWORD will
not cause loss of any data bits.

What happens if converting DWORD to WORD?

How to convert "safely" a DWORD that has a value within the range of WORD?

Thanks!

Re: Convert between DWORD and WORD by Doug

Doug
Sat Jun 19 15:36:36 CDT 2004

pat wrote:

>My understanding is that conversion of WORD type data converts to DWORD will
>not cause loss of any data bits.
>
>What happens if converting DWORD to WORD?
>
>How to convert "safely" a DWORD that has a value within the range of WORD?

Just cast it, e.g. static_cast<WORD>(my_dword). If the DWORD value is within
the range of WORD, this produces a WORD with the same value. If the DWORD
value is outside the range of WORD, the result of the cast will be the least
value of WORD congruent to the DWORD modulo 2^16. This is because WORD is an
unsigned 16 bit integer type. Because DWORD is a 32 bit unsigned integer,
you can think of the conversion to WORD as discarding the upper 16 bits of
the DWORD.

--
Doug Harrison
Microsoft MVP - Visual C++