Micromouse Online


Home
Tuesday, 06 January 2009
Main Menu
Home
Blog
News
Forum
Articles
Gallery
FAQs
Links
Search






Lost Password?
Syndicate
Integer promotion in C30 PDF Print
User Rating: / 0
PoorBest 
Written by Pete Harrison   
Saturday, 07 July 2007

I was having a browse through the book 'Programming 16-bit Microcontrollers in C. Learning to Fly the PIC24' by Lucio Di Jasio.

Leaving aside for now the tendency for authors to use overlong titles, there seems to me some interesting stuff in there. Early on there is a bit about what happens when you assign an int to a long as in this code fragment:

int intvar;
long longvar;
longvar = intvar;

In the book it says that the int is copied into the lower 16 bits of the long and the upper 16 bits are filled with zeros. Well, Iwould hope not. At the very least, I would expect that the sign would be extended into the upper 16 bits. After bashing this code into a file in MPLAB, I compiled it and looked at the generated assembly code. In fact, what the compiler does is to multiply the int by 1 and assign the resulting 32bit value to the long. This will have the desired effect. I have not bothered to see if the dsPIC has a sign extend instruction but clearly the compiler thinks this is the most efficient way todo the job.

 

Last Updated ( Saturday, 07 July 2007 )
 
Builder's Blog
Latest News
Forthcoming Events
There are no upcoming events currently scheduled.
Events Calendar
December 2008 January 2009 February 2009
Su Mo Tu We Th Fr Sa
Week 1 1 2 3
Week 2 4 5 6 7 8 9 10
Week 3 11 12 13 14 15 16 17
Week 4 18 19 20 21 22 23 24
Week 5 25 26 27 28 29 30 31
Who's Online
Adapted from a design by Joomlateam.com