EmbDev.net

Forum: ARM programming with GCC/GNU tools parentheses around logic operands


von Roman M. (romez777)


Rate this post
useful
not useful
Hello,

I'm implementing I2C interface. I took Atmel's TWI code for reference.
It gets compiled perfectly except one spot and warning accompaning it:

/* Set the TWI Master Mode Register */
pTwi->TWI_MMR =  SlaveAddr | IntAddrSize & ~AT91C_TWI_MREAD;

warning: suggest parentheses around arithmetic in operand of |

I understand the priority of '&' operator is higher then others. So how
should I place parentheses correctly (it may give various results)?

Datasheet is not very eloquent about this.

Thanks in advance.

von Clifford S. (clifford)


Rate this post
useful
not useful
It is merely suggesting that you are explicit about your intent so write
either:

pTwi->TWI_MMR =  SlaveAddr | (IntAddrSize & ~AT91C_TWI_MREAD);

or

pTwi->TWI_MMR =  (SlaveAddr | IntAddrSize) & ~AT91C_TWI_MREAD;

depending upon what you intended.

You are right that if you intended the first form then that is identical
to your original code, but it is as well to be explicit because bitwise
operator precedence often catches the unwary.

This warning is enabled by the -Wparentheses option, and in later
versions of GCC by -Wall.
http://gcc.gnu.org/onlinedocs/gcc-4.2.2/gcc/Warning-Options.html#index-Wparentheses-228


Clifford

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.