View previous topic :: View next topic |
Author |
Message |
Riddick
Joined: 09 Oct 2007 Posts: 9
|
LOWORD & HIWORD |
Posted: Sun Nov 04, 2007 11:32 pm |
|
|
Hi guys,
I thought I might throw this little scrap of code up, more for scrutinisation from the more seasoned developers than anything. But it may be something that is useful for newer prorgammers, especially those who've done a little with the Win32SDK.
I'd not used unions before. If there is a better way to do what I've done I'd certainly appreciate the feedback.
Code: | //The following allows a 32bit value to
//be accessed as 2 16bit values.
//
// eg:
//
// PARAM cParam;
//
// cParam.DWORD = 0x11223344;
// cParam.LOWORD = 0;
// cParam.HIWORD++;
//
// (cParam.DWORD now equals 0x11230000)
//
//
struct _16b{
int16 iLo;
int16 iHi;
};
typedef union{
int32 DWORD;
struct _16b i16;
}PARAM;
#define LOWORD i16.iLo
#define HIWORD i16.iHi |
It could be expanded to allow easy access to each individual byte, but my application does not require this. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Jul 14, 2009 4:12 pm |
|
|
I think this works too (compiles anyway -- will test in a bit)
Code: |
static union word_bits {
struct byte_map {
unsigned int lo;
unsigned int hi;
};
unsigned long word;
};
|
Not the same as what you're doing - but similar in concept.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Tue Jul 14, 2009 11:24 pm |
|
|
I know you chaps are messing about with structures and stuff (something that I'm not very comfortable with), but if it's just accessing high and low words of an int32 that is required, then this snippet of code works nicely also:
Code: | int32 var=0x89abcdef;
int16* p1=0;
int16 hi=0;
int16 lo=0;
p1=&(var);
lo=*p1; //lo byte of a variable stored at
p1++; //lower memory location
hi=*p1; |
A few days back I had a major headache debugging 'pointy' pointer code So now I can 'certify' that this works fine
I haven't compared the disassembly listing for the two codes, but I suppose that they achieve the result in nearly the same manner.
Rohit |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jul 15, 2009 2:04 am |
|
|
Rohit de Sa wrote: |
lo=*p1; //lo byte of a variable stored at
Rohit |
You mean low WORD! |
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Wed Jul 15, 2009 4:21 am |
|
|
Quote: | You mean low WORD! | int16, int8, int32.....arrgh!!! Absolutely right there, Wayne That's one of the reasons I explicitly use 'int8' instead of just 'int' in PCM, and 'int16' instead of just 'int' in PCD, to define variables.
Rohit |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Wed Jul 15, 2009 9:29 am |
|
|
Well, I *tend* to use INT and LONG when I'm in microcontrollerland because I know in CCS what those mean across all the lines of the compiler.
But I have seen confusion for others kick in when they ponder that an INT can be 8/16/32 depending on the compiler.
It's a real toss up.
I suppose CCS wouldn't have created make8, make16 and make32 to help sort the confusion if it didn't exist.
(shrug) _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|