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 CCS Technical Support

Help with combining two data types into one

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



Joined: 09 Jun 2013
Posts: 153

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

Help with combining two data types into one
PostPosted: Sat Aug 30, 2014 2:07 am     Reply with quote

CCS C 4.130
dsPIC33 EP 512 GP 806


ok, I'm finding this a little frustrating, I use GCC to work on a routine, it normally works out very well on the pc. But when i come to port it to the chip. ccs.. it compiles but behaves differently!

the computer I'm just doing basic...
Code:

uint8_t a, foo;

uint8_t ans;

a = 127;
foo = 127;
ans = a + foo; // 254 right?


this works in ccs also.
BUT..

i am doing:
Code:
uint8_t foo;
uint16_t man;

uint32_t fingers;

fingers = foo + man;

the answer comes out wrong most of the time in CCS,

But in GCC (more relaxed maybe?) but gets the answers correct!
its the same with MPLABS too.

am i right in having to state
Code:
fingers = (uint32_t)foo + (uint32_t)man;

???

Thanks for the advice Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Sat Aug 30, 2014 4:02 am     Reply with quote

This has been covered many times.

In C, the 'rule' is that maths type actually used, is the 'highest' of the two 'source' types in an expression. So if you add/multiply etc., an int8, by an int16, int16 maths will be used, even if the target is an int32.

However, on the PC, maths in general is done using the hardware maths unit, and this automatically 'extends' the type, if there is an overflow. Most C's will also do this, but CCS elected not to, to keep the maths code as small as possible. So on CCS, it is totally strict about sizes, and you need to cast to ensure that the type needed is used (only one value needs to be cast).
neochrome32



Joined: 09 Jun 2013
Posts: 153

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

PostPosted: Sat Aug 30, 2014 11:12 am     Reply with quote

Thanks Ttelmah,


i did a search for this, but no result...
MPLAB appear to have elected it also then.

ok back to casting (type)value; it is.....

Man you know a lot!! i thought i knew C, C++ but I STILL fall into these traps!!!

thanks again Smile also, thanks for the advice about the audio issue... i solved it, turns out im getting the noise while accessing the SDCARD so... looks like im rethinking the SDCARD dat lines... (it works, but not as fast as id like)
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