avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Strange issues with 16-bit values


From: Brian Neltner
Subject: Re: [avr-gcc-list] Strange issues with 16-bit values
Date: Tue, 11 Nov 2008 13:14:09 -0500

On Tue, 2008-11-11 at 12:16 -0600, David Kelly wrote:
> You use 224UL in your expression and wonder why the comparison is
> promoted to 32 bits? The comparison is always the size of the biggest
> and your cast only sticks to the first item.

Aha! Thanks =) I'm a novice at C (my job is in bioengineering), and
didn't understand that casting only applied to the first item. In
retrospect, this makes a lot more sense!

Thanks for the tips!

-Brian

> If you want to force it down to a 16 bit comparison you need to force
> everything down to 16 bits:
> 
> while(TCNT3<(uint16_t)( benc_period*224UL/255) );
> 
> But I think it would be best written like this to enforce the desired
> evaluation order:
> 
> while( TCNT3 < (uint16_t)( (benc_period*224UL) /255) )
>       ;
> 
> The above because I would be concerned the compiler might rearrange the
> order and precalculate 224UL/255. Always better safe than sorry.
> 
> On empty while() loops I like to put the semicolon on a separate line
> so that the emptiness stands out.
> 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]