开发者

Would Java interpret this boolean expression in the way I wanted it to?

This is what I want: !A || (A && B && C) Is this equivalent t开发者_如何学Pythono the original? !A || A && B && C why or why not?


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.


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.


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.


&& 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.


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");
    }

}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜