11.07.2015 Views

PicC 9.50 dsPIC Manual.pdf

PicC 9.50 dsPIC Manual.pdf

PicC 9.50 dsPIC Manual.pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C Language FeaturesOperatorsare not aware that these changes of type have taken place, the results of some expressions are notwhat would normally be expected.Integral promotion is the implicit conversion of enumerated types, signed or unsigned varietiesof char, short int or bit-field types to either signed int or unsigned int. If the result of theconversion can be represented by an signed int, then that is the destination type, otherwise theconversion is to unsigned int.Consider the following example.unsigned char count, a=0, b=50;if(a - b < 10)count++;The unsigned char result of a - b is 206 (which is not less than 10), but both a and b are convertedto signed int via integral promotion before the subtraction takes place. The result of thesubtraction with these data types is -50 (which is less than 10) and hence the body of the if() statementis executed. If the result of the subtraction is to be an unsigned quantity, then apply a cast.For example:if((unsigned int)(a - b) < 10)count++;The comparison is then done using unsigned int, in this case, and the body of the if() would notbe executed.Another problem that frequently occurs is with the bitwise compliment operator, “~”. Thisoperator toggles each bit within a value. Consider the following code.unsigned char count, c;c = 0x55;if( ~c == 0xAA)count++;If c contains the value 55h, it often assumed that ~c will produce AAh, however the result is FFAAhand so the comparison in the above example would fail. The compiler may be able to issue amismatched comparison error to this effect in some circumstances. Again, a cast could be used tochange this behaviour.The consequence of integral promotion as illustrated above is that operations are not performedwith char-type operands, but with int-type operands. However there are circumstances when theresult of an operation is identical regardless of whether the operands are of type char or int. Inthese cases, HI-TECH C will not perform the integral promotion so as to increase the code efficiency.Consider the following example.43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!