473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7130
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a tuple OR as the number 5 in a mathematical...
4
by: August Karlstrom | last post by:
Hi, How come the modulus operator doesn't always yield non-negative values? I expect e.g. (-1) % 3 to be 2 but I get -1. If I want to decrement a cyclic variable n by k I have to write something...
3
by: Frederick Gotham | last post by:
If you wish to override the natural operator precedence and associativity in C, you can use parentheses: 5 * (4 - (2 + 7)) However, I was surprised to discover (by testing) yesterday that some...
4
by: Thelma Lubkin | last post by:
I've inherited code with SELECT statements that look like this one: SELECT tblSales.YEAR As , Count(tblSales.UNITS) As FROM INNER JOIN tblSales ON .STFID = tblSales.STFID WHERE...
13
by: Gary Wessle | last post by:
Hi in order to assign a value to a variable if a switch is true, can I do this switch && var=val; instead of if( switch == true ) var = val; and be safe? thanks
3
by: Lyn | last post by:
Can anyone explain this for me? A sub procedure can be called with or without parentheses around the arguments. By personal convention, I normally use parentheses. I am in the middle of a...
3
by: Helen Wheels | last post by:
Can we use parentheses in a check constraint in MS-SQL-server DDL? e.g. I'm having a problem with the following statement: ALTER TABLE . ADD CONSTRAINT CHECK (( IS NULL AND IS NULL) OR (...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
7
by: vlsidesign | last post by:
I am not sure how to think about parentheses. It seems when they are used alongside characters it indicates a function, like addTwoNums(). But is also is used to enforce which operators and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.