parentheses in logic operands

Discussion in 'C Programming' started by vippstar@gmail.com, Jan 3, 2008.

  1. Guest

    On Jan 4, 2:32 am, "Roman Mashak" <> wrote:
    > 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, 2008
    #1
    1. Advertising

  2. SM Ryan Guest

    "Roman Mashak" <> wrote:

    # warning: suggest parentheses around arithmetic in operand of |

    # 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 compiler warning is based on years of experience
    of stupid humans not understanding the code they are
    looking at. It's directed at making the code more
    comprehensible to humans not computers.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Title does not dictate behaviour.
     
    SM Ryan, Jan 3, 2008
    #2
    1. Advertising

  3. Roman Mashak Guest

    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:
     
    Roman Mashak, Jan 4, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chao
    Replies:
    6
    Views:
    1,269
    Mike Treseler
    Jul 5, 2004
  2. A. Kong
    Replies:
    3
    Views:
    17,548
    Alan Fitch
    Oct 11, 2004
  3. Jerry Krinock
    Replies:
    6
    Views:
    364
    Jerry Krinock
    Feb 18, 2005
  4. Frank Buss
    Replies:
    5
    Views:
    10,274
    Duane Clark
    Jul 1, 2006
  5. spike
    Replies:
    8
    Views:
    1,471
    Steve Holden
    Feb 9, 2010
Loading...

Share This Page