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

Author: Roman Mashak (romez777)
Posted on:

Rate this post
0 useful
not useful

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.

Author: Clifford Slocombe (clifford)
Posted on:

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

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


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.



Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.