468,512 Members | 1,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

parentheses in logic operands

Hello,

'gcc' complains about this code:

#define TWI_MREAD ((unsigned int) 0x1 << 12)
#define TWI_IADRSZ_1_BYTE ((unsigned int) 0x1 << 8)

int write_single(int SlaveAddr, int IntAddrSize)
{
unsigned int mmr = SlaveAddr | IntAddrSize & ~TWI_MREAD;
...
}

....
write_single(0x22, TWI_IADRSZ_1_BYTE);
....

warning: suggest parentheses around arithmetic in operand of |

Certainly priority of '&' operation is higher then '|'. Should I put
parentheses like this:

SlaveAddr | (IntAddrSize & ~TWI_MREAD)

or

(SlaveAddr | IntAddrSize) & ~TWI_MREAD

It seems it has just logical meaning (and convenience of reading the code),
but result is same. Or it might have any side effects?

Thanks in advance.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Jan 3 '08 #1
1 6930
On Jan 4, 2:32 am, "Roman Mashak" <m...@tusur.ruwrote:
It seems it has just logical meaning (and convenience of reading the code),
but result is same. Or it might have any side effects?
The result is not the same.
Consider this

(2 | 1) & 1 -1
2 | (1 & 1) -3
2 | 1 & 1 - ? # notice it's not undefined behavior
# just harder for the programmer to figure it out

And that is why gcc suggests parenthesis around.
Jan 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by August Karlstrom | last post: by
3 posts views Thread by Frederick Gotham | last post: by
4 posts views Thread by Thelma Lubkin | last post: by
13 posts views Thread by Gary Wessle | last post: by
3 posts views Thread by Helen Wheels | last post: by
7 posts views Thread by vlsidesign | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.