Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting 2 Words From a Double Word

Status
Not open for further replies.

PulsarSL

Programmer
Jul 4, 2005
24
NO
Hi guys,

I'm using the Windows MCI to write a program. One of the status commands I'm using returns information I need about the levels in both the left and right audio channels in a dword. The low-order word is the right channel and the high-order word is the left channel.

How do I seperate the dword into the component words?

Thanks,

Pulsar
 
You could use the macros LOWORD and HIWORD. You could use masks and shifts but that depends on whether you are working in a 16, 32 or 64 bit environment and whether the data that is coming back comes from a 16, 32 or 64 bit system.

On 16 bits, everything was sane, words were 16 bits, DWORDS were 32 bits. On 32 bits, both WORDs and DWORDs are both 32 bits. No idea what they are on 64 bits.

If we assume wsize is the word size and wmask is the word mask, then
Code:
WORD wsize = 16; // assuming 16 bit env
WORD wmask = 0xFFFF;
WORD loword = (WORD)(dw & wmask);
WORD hiword = (WORD)((dw >> wsize) & wmask);
 
Thanks for the prompt response... I'll try the macros.

Pulsar
 
On 32 bits, both WORDs and DWORDs are both 32 bits.

WORD itself is a 16bit "structure". Like BYTE will never become 16bit or 32bit. I really doubt Microsoft would start making such drastic macro redefinitions. Can you please paste in some proof of where WORD is considered a 32bit value?

------------------
When you do it, do it right.
 
Another approach would be to use a union defined like this:

typedef struct
{
WORD w1;
WORD w2;
}MyDWORD;

typedef union {
MyDWORD aDWORD;
DWORD dwValue;
}NameMe;

The trick is that the union occupies as much as its largest member (which in this case is the aDWORD variable) and that all the data occupies the same memory location.
So, you can put a DWORD value in the dwValue member and then read the two WORDs from aDWORD.w1 and aDWORD.w2
Should work both ways.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top