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

Build 32 Bit Number from Non-8bit aligned vars

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



Joined: 23 Feb 2006
Posts: 6

View user's profile Send private message

Build 32 Bit Number from Non-8bit aligned vars
PostPosted: Wed Mar 29, 2006 3:10 pm     Reply with quote

CCS Version: CCS PCM C Compiler, Version 3.242, 32566

Just wondering if anyone can suggest a better way to build a 32bit number from a couple vars, but these other vars are not 8bit vars.

idealy I would have done something like:

typedef struct {
int16 i13Var : 13;
int8 i5Var : 5;
int16 i9Var : 9;
}myBigVar;

but I guess you can't do that with 8bit vars in CCS

sooo.....instead maybe this:

inline int32 SomeFunction(int16 i13Var, int8 i5Var, int16 i9Var)
{

int32 myBigVar = 0x0;
int32 i13Var_t = 0;
int32 i5Var_t = 0;
int32 i9Var_t = 0;

i9Var_t = (i9Var & 0x01FF);
i5Var_t = ( ( (int32)i5Var ) << 9 );
i13Var_t = ( (int32)i13Var << 14 );

myBigVar = i13Var_t + i5Var_t + i9Var_t;

return myBigVar;
}

The above seems to work, but if I put it all on one line, it doesn't

myBigVar = (i9Var & 0x01FF) + ( ( (int32)i5Var ) << 9 ) + ( (int32)i13Var << 14 );



example of use:
int32 i32Addr = 0x00;

int16 iBlock = 0b1111111111111;
int8 iNAND = 0b11111;
int16 iByte = 0b111111111;

i32Addr = scMake32Addr(iBlock,iNAND,iByte);

---results---
i32Addr ==> 0x07FFFFF -- correct

but the one line version results in
i32Addr ==> 0x0000C0FF -- incorrect


Any ideas on why the one liner version doesn't work?
Any Better ways of doing this (I really don't like the type case to 32bit because it's adding overhead)

disclaimer: I just type up the above code, so there _might_ be mistakes..but the code I'm actually use compiles and works (except as mentioned)


Thanks
Gary
[/code]
crumgary



Joined: 23 Feb 2006
Posts: 6

View user's profile Send private message

PostPosted: Wed Mar 29, 2006 3:28 pm     Reply with quote

ok, I lied, the one-liner version works too!

I had the MPLAB simulator set for a different chip than I was compiling for.
You would think that after I had a simular mistake with the simulator before, I would have made sure of that setting.... doh

So, I guess I was just wondering if there is a better way to do this.

Thanks again
Gary
crumgary



Joined: 23 Feb 2006
Posts: 6

View user's profile Send private message

PostPosted: Fri Mar 31, 2006 2:21 pm     Reply with quote

no thoughts on this?
Thanks
Gary
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