布尔运算符,当两个相似的值相等时返回true。

huangapple go评论64阅读模式
英文:

Boolean operator which gives true from two similar values

问题

给定两个布尔变量x和y,操作符应该是这样的:

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

以下断言应该通过:

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

是否有适用于此的操作符?

编辑。

抱歉,我忘记提到除了等式检查之外的操作符。我正在寻找一种可以像一般逻辑门中那样给出组合结果的操作。我只想知道是否可能。如果可能,这个操作是什么。

英文:

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.

答案1

得分: 4

是的,可以使用==操作符来实现这个功能。

请查看下面的解释:

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

演示:

public class Main {
    public static void main(String[] args) {
        // 测试
        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;
    }
}

输出:

true
true
false
false

或者,您也可以通过取位异或(bitwise exclusive OR)操作的否定来获得相同的结果,如下所示:

public class Main {
    public static void main(String[] args) {
        // 测试
        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);
    }
}

输出:

true
true
false
false
英文:

> 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

答案2

得分: 1

你只需要检查这两个值是否相等。

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

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

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

huangapple
  • 本文由 发表于 2020年10月6日 13:44:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64219996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定