views:

137

answers:

6

This is what I want: !A || (A && B && C) Is this equivalent to the original? !A || A && B && C why or why not?

A: 

&& have precedence than || . the two expressions are the same. because A && B && C will be evaluated first

Here's your operator Precedence guid in Java.

youssef azari
R. Bemrose
Yes, that's what i'm saying =)
youssef azari
@youssef azari: Correction, that's what you're saying *now*... which reminds me, I need to stop by meta and file a new feature request on edits made after someone replies with a comment.
R. Bemrose
I did a mistake so I edited 3 secondes after i've posted. so +1 for the feature request, posts should be periodicaly refreshed.
youssef azari
+7  A: 

Yes, they are the same. So is the simplified !A || B && C.

The && operator has higher precedence than the || operator, so the parentheses around the && operation are unnecessary.

erickson
A: 

To be sure and to be better readable I would suggest to put parantheses around the operations to make the precedence clear.

EDIT: This is not only my opinion. It is a recomondation according to MISRA-c (Rule 34) See electronicDesing at the bottom.

And since the precendence of && is over || they are the same.

schoetbi
Adding redundant parentheses doesn't add any sureness, and whether it adds readability is a question of taste.
EJP
Check this out: http://electronicdesign.com/article/embedded/misra-c-safer-is-better2824.aspxIt is a recomondation to create code that is better readable.
schoetbi
JLS (Evaluation Order): "It is recommended that code not rely crucially on this specification."
Carlos Heuberger
@shoetbi: That's a coding standard for C embedded into vehicles, explicitly motivated by issues that don't arise in Java. Rule 34 is motivated by the lack of booleans in C. And I don't find it more readable in the least.@Carlos: That JLS statement specifically refers to the left-to-right evaluation rule, in the context of side-effects and exceptions. Not to operator precedence. In reality every Java program relies 'crucially' on the Java specification. They're really not going to change the operator precedence on us.Maybe it's just me. I never have these problems.
EJP
+2  A: 

Yes it would.

Have a look at the Java operator precedence.

As you'll notice, logical AND has higher precedence than logical OR which means that && "binds tighter than" ||. Furthermore unary ! also has higher precedence than || so it will only affect the initial A.

aioobe
+2  A: 

Yes the 2 expressions are equal. The reason that you don't need parenthesis in this specific expressions is that the && operator has priority over the || operator.

Edit: Here's a nice unit test that checks the equality for all possible values:

package main;

import junit.framework.Assert;

public class Test
{
    enum BOOL
    {
        TRUE(true), FALSE(false);

        private boolean m_val;

        BOOL(boolean val)
        {
            m_val = val;
        }

        public boolean getValue()
        {
            return m_val;
        }
    }

    @org.junit.Test
    public void test1()
    {
        boolean a,b,c;
        for (BOOL boola : BOOL.values())
        {
            for (BOOL boolb : BOOL.values())
            {
                for (BOOL boolc : BOOL.values())
                {
                    a = boola.getValue();
                    b = boolb.getValue();
                    c = boolc.getValue();
                    boolean exp1 = !a || (a && b && c);
                    boolean exp2 = !a || a && b && c;
                    Assert.assertEquals(exp1, exp2);
                }
            }
        }
    }
}

The test passes, so the 2 expressions are indeed equal.

duduamar
You could also write `for (boolean b : new boolean[] { true, false} )`.
aioobe
Well of course you're right. For some reason enums were the first thing that came to mind :)
duduamar
A: 

Operator precedence of a bitwise and (&&) is higher than the bitwise or (||). See http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

This means both logical expressions are equal as the A && B && C part of the second logical epxression is evaluated first then it is combined with the !A as you can see in the following table where F1 is your first expression and F2 your second

A   B   C   F1  F2 
0   0   0   1   1
0   0   1   1   1
0   1   0   1   1
0   1   1   1   1
1   0   0   0   0
1   0   1   0   0
1   1   0   0   0
1   1   1   1   1

Following is also the java code to test this

public class TestLogic {
    public static void main(String[] args) {
        for (int i=0;i<8;i++) {
            boolean a=bit(i,3);
            boolean b=bit(i,2);
            boolean c=bit(i,1);
            System.out.println(asZeroOne(a)+asZeroOne(b)+asZeroOne(c)+"\t"+asZeroOne(f1(a,b,c))+"\t"+asZeroOne(f2(a,b,c)));
        }
    }

    static boolean f1(boolean a, boolean b, boolean c) {
        return !a || (a && b && c); 
    }

    static  boolean f2(boolean a, boolean b, boolean c) {
        return !a || a && b && c;
    }

    static  boolean bit(int value, int bitnumber) {
        int bitmask =  (int) Math.pow(2, bitnumber-1);
        return (value & bitmask)>0;
    }
    static String asZeroOne(boolean b) {
        return (b?"1":"0");
    }

}
mrjohn