tags:

views:

312

answers:

2

Is there an operator in Java that will give a result of false if either conditions are false, but if both are true or both false the result will be true?

I have some code that relies on a user entering some values for a process to run. As the user should only be able to enter x or y but not both or none I would like to show an error message in this case.

+8  A: 

You want XNOR, basically:

if (!(a ^ b))

or (more simply)

if (a == b)

where a and b are the conditions.

Sample code:

public class Test
{
    public static void main(String[] args)
    {
        xnor(false, false);
        xnor(false, true);
        xnor(true, false);
        xnor(true, true);
    }

    private static void xnor(boolean a, boolean b)
    {
        System.out.printf("xnor(%b, %b) = %b\n", a, b, a == b);
    }
}

Produces this truth table;

xnor(false, false) = true
xnor(false, true) = false
xnor(true, false) = false
xnor(true, true) = true
Jon Skeet
Jon, it would probably be beneficial to show the full logic table for the XOR operator in this case. I would do it here, but the comment formatting is horrible for this.
jdmichal
Thanks, that was what I was looking for! I knew there was an operator, just couldn't remember it :)
James
@James: I've edited this with a simpler way of doing this - the equality operator!
Jon Skeet
Also, yes, `!=` is simpler. But !XOR is the correct boolean operator for this case. So I still like your answer the best.
jdmichal
That definition of xnor as !(A ^ B) looks a bit unusual, (A ^ B) v (!A ^ !B)
Andrew
@jdmichal: != is actually wrong; that gives XOR instead of XNOR. @Andrew: I think of XNOR as the inverse of XOR, leading to that representation. It has the advantage of only evaluating each condition once...
Jon Skeet
@Jon, running True,True through your expression though gives !(true and true) = false, unless you mean't something else in the ascii representation.
Andrew
My quick-type errors aside, I still believe what I stated.
jdmichal
@Andrew: I don't see what you mean. true ^ true is false, so !(true ^ true) is true. I never wrote "and". Could you clarify?
Jon Skeet
Your ascii representation ^ is meaning something other than 'and', which would be the common interpretation of that symbol. Consequence of ascii vs logic symbols.
Andrew
This question was given in the context of Java. In Java, the `^` symbol represents the bit-operation XOR.
jdmichal
@Andrew: It's not an *ASCII* representation - it's code. It's an "if" condition, in the context of a Java question. I haven't used the symbol ^ anywhere other than in a code font, for a real snippet of code, in a question about coding.
Jon Skeet
My mistake, I was reading it as a logical statement independant of a language. When I wrote the original comment, I put in XNor as it would be expressed in a mathematical statement.
Andrew
A: 

well if x and y are bools, you could just check:

boolean valid = x != y;
David Hedlund
Wrong way round - it's XNOR which is required, not XOR.
Jon Skeet