[英]Optimize the if condition
I want to simplify this condition where a and b are booleans 我想简化这种情况,其中a和b为布尔值
!((a && b) || (!a && !b))
I have tried using the truth table generator, but I am not able to figure out the simplified version. 我曾尝试使用真值表生成器,但无法找出简化版本。
This can be simplified as a XOR
(exclusive or). 这可以简化为
XOR
(异或)。 It's equivalent to (a && !b) || (!a && b)
相当于
(a && !b) || (!a && b)
(a && !b) || (!a && b)
. (a && !b) || (!a && b)
。
In Java, it can be achieved as a ^ b
在Java中,可以将其实现为
a ^ b
You can simplify this in : a!=b
, 您可以简化为:
a!=b
,
Because, by equivalence : 因为,等效地:
!((a && b) || (!a && !b)) // not(both true or both false)
!(a==b) // not(both equals)
a!=b // different
And here is the table, which confirms that you a
different of b
: 这里是表,它证实您
a
不同的b
:
a\b | true | false
true | false | true
false | true | false
You could simplify to either if (!(a==b))
or if (a!=b)
. 您可以简化为
if (!(a==b))
或if (a!=b)
。
Your condition simplifies to "if a and b are both true (ie, of equal state) or both false, then condition resolves to false". 您的条件简化为“如果a和b均为真(即状态相同)或均为假,则条件解析为假”。 Or "if a is a different state to b, condition resolves to true".
或“如果a是与b不同的状态,则条件解析为true”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.