View previous topic :: View next topic |
Author |
Message |
davt
Joined: 07 Oct 2003 Posts: 66 Location: England
|
casting problem? |
Posted: Thu Nov 18, 2004 5:21 am |
|
|
Hi all
My problem is ' value ' will not accept anything over 255.
value is a unsigned int.
value=(int16)((hundreds*0x64)+(tens*0x0a)+(units));
Thanks for your help.
Dave |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Thu Nov 18, 2004 5:32 am |
|
|
That is because an unsigned int is only 8 bits wide
value should be declared as unsigned int16 _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
davt
Joined: 07 Oct 2003 Posts: 66 Location: England
|
|
Posted: Thu Nov 18, 2004 5:36 am |
|
|
Thanks Andrew
Sorry I made a mistake - value is a unsigned int 16.
Thanks.
Dave |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Thu Nov 18, 2004 5:42 am |
|
|
how are the other variables declared? _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
davt
Joined: 07 Oct 2003 Posts: 66 Location: England
|
|
Posted: Thu Nov 18, 2004 5:47 am |
|
|
The other 3 values are declared as unsigned char. |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 18, 2004 6:17 am |
|
|
davt wrote: | The other 3 values are declared as unsigned char. |
In which case your 'sum' should appear as:
value=((int16)hundreds*0x64)+((int16)tens*0x0a)+(units);
The problem is that you are taking an 8bit type, and an 8bit constant (this will be assumed, unless the constant if being involved in 16bit arithmetic, or has 'L' appended), and inherently then performing 8bit maths. Then at the end, you convert the result to an int16. You need to convert the individual 'values' to int16, thereby forcing 16bit arithmetic to be used.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Nov 18, 2004 7:54 am |
|
|
For readability I would specify your constants in decimal. It makes no difference to the compiler or the resulting code, but is easier on the humans. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
davt
Joined: 07 Oct 2003 Posts: 66 Location: England
|
|
Posted: Thu Nov 18, 2004 10:50 am |
|
|
Thanks Ttelmah that did the trick!!
And sherpadoug - point taken!
Regards.
Dave |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 18, 2004 11:02 am |
|
|
davt wrote: | Thanks Ttelmah that did the trick!!
And sherpadoug - point taken!
Regards.
Dave |
It is an area where great 'care' is needed, since the CCS compiler does not promote types forward in the way many C implementations do. The advantage of this, is that smaller code can be used by keeping the types small where required, but it prevents the simple automatic solution (on most C's, if you multiply two 8 bit types, and assign the result to a 16bit variable, the 'highest type' will be used, and the maths will be 16bit.
So long as you are aware of it (cast as necessary), the CCS solution makes a lot of sense for a microcontroller application.
Glad it is working now.
On Sherpadoug's comment, I have allways wondered if the 'number' in the Hitch-hikers guide to the galaxy, was actually an incorrect conversion from hex, and was trying to crack a joke on '2B or not 2B'. :-)
Binary makes 'sense', when coding for a 'mask', or a value fed to the individual bits on a port.
Hex makes sense when dealing with 'bytewide' values of a similar sort.
Decimal is by far the clearest when doing decimal conversions.
Best Wishes |
|
|
|