xploreraj
xploreraj

Reputation: 4362

Boolean operator which gives true from two similar values

Given two boolean variables x and y, the operator should be such that

boolean getResult(boolean x, boolean y) {
    return x op y;
}

the following assertions pass

assertEquals(getResult(true, true), true);
assertEquals(getResult(false, false), true);
assertEquals(getResult(true, false), false);
assertEquals(getResult(false, true), false);

Is there any operator for this?

Edit.

Sorry, I missed mentioning operator other than equality check. I'm looking for some kind of operation which can give a combined result like we have in general logical gates. I just want to know if its possible or not. If possible, whats the operation.

Upvotes: 1

Views: 379

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79035

Is there any operator for this?

Yes, the operator for this can be ==.

Check the explanation given below:

true == true => true
false == false => true
true == false => false
false == true => false

Demo:

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getResult(true, true));
        System.out.println(getResult(false, false));
        System.out.println(getResult(true, false));
        System.out.println(getResult(false, true));
    }

    static boolean getResult(boolean x, boolean y) {
        return x == y;
    }
}

Output:

true
true
false
false

Alternatively, you can get the same result by negating bitwise exclusive OR operation as demonstrated below:

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getResult(true, true));
        System.out.println(getResult(false, false));
        System.out.println(getResult(true, false));
        System.out.println(getResult(false, true));
    }

    static boolean getResult(boolean x, boolean y) {
        return !(x ^ y);
    }
}

Output:

true
true
false
false

Upvotes: 4

Sahan
Sahan

Reputation: 44

You just have to check if the two values are equal.

boolean getResult(boolean x, boolean y) {
 return x === y;
}

Upvotes: 1

Related Questions