|
|
View previous topic :: View next topic |
Author |
Message |
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
Help with combining two data types into one |
Posted: Sat Aug 30, 2014 2:07 am |
|
|
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 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Sat Aug 30, 2014 4:02 am |
|
|
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
|
|
Posted: Sat Aug 30, 2014 11:12 am |
|
|
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 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) |
|
|
|
|
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
|