CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

LOWORD & HIWORD

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Riddick



Joined: 09 Oct 2007
Posts: 9

View user's profile Send private message

LOWORD & HIWORD
PostPosted: Sun Nov 04, 2007 11:32 pm     Reply with quote

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. Smile


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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 4:12 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 14, 2009 11:24 pm     Reply with quote

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 Very Happy So now I can 'certify' that this works fine Razz

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

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 2:04 am     Reply with quote

Rohit de Sa wrote:

lo=*p1; //lo byte of a variable stored at
Rohit


You mean low WORD! Smile
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 15, 2009 4:21 am     Reply with quote

Quote:
You mean low WORD!
int16, int8, int32.....arrgh!!! Razz Absolutely right there, Wayne Very Happy 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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 9:29 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group