[英]Can !a!=!!b^!!-!a||!+!a|!c return anything other than 1?
I was playing the Javascript game with somebody and we were having fun making ridiculous and absurd expressions to make our inputs get a particular output. 我正在和某人一起玩Javascript游戏,我们很开心制作荒谬和荒谬的表达方式,让我们的输入获得特定的输出。
This little charming one 这个小迷人的
!a!=!!b^!!-!a||!+!a|!c
always seemed to return 1
. 似乎总是回归
1
。 I tried to reason it out, but I gave up after losing track of all the !
我试图推理出来,但是在失去所有的轨道后我放弃了
!
s. 秒。
Are there any values for a
, b
, and c
which do not return 1
? 是否有
a
, b
和c
值不返回1
? If not, why does it always return 1
? 如果没有,为什么它总是返回
1
?
Short answer, yes. 简短的回答,是的。
a = false, b = false, c = true
is a counter-example because your equation is identical to (!!a || !!b || !c)
. a = false, b = false, c = true
是一个反例,因为你的方程与(!!a || !!b || !c)
。
Long answer: 答案很长:
!a!=!!b^!!-!a||!+!a|!c
is 是
(((!a) != (!!b)) ^ (!!(-!a))) || ((!+!a)|!c)
which reduces to 减少到
((Boolean(a) == Boolean(b)) ^ (!a)) || (Boolean(a) | !c)
so all of a
, b
and c
are only dealt with as truthy/falsey values and the result must be a 1
or 0
since |
所以
a
, b
和c
都只作为truthy / falsey值处理,结果必须是1
或0
因为|
and ^
both coerce booleans to numbers. 和
^
都将布尔值强加给数字。
So obviously (from inspection of the right of the ||
) if either a
is truthy or c
is falsey, you get 1
. 所以很明显(通过检查
||
的权利)如果a
是真或者c
是假的,你得到1
。
If a
is falsey and c
is truthy, you have two possibilities, 如果
a
是假的而c
是真的,你有两种可能性,
b
is truthy in which case the ^
clause is 1
so the right of the ||
b
是真实的,在这种情况下, ^
子句是1
因此||
的右边 is never reached. b
is falsey, in which case the ^
clause is 0
so the right of the ||
b
是假的,在这种情况下^
子句是0
所以||
的右边 dominates to produce 0
. 0
。 How about this: 这个怎么样:
var a = undefined, b=undefined, c=!a
alert(!a!=!!b^!!-!a||!+!a|!c)
// Output: 0
Did you even try running it in a few loops: 你有没有尝试在几个循环中运行它:
for(var a = 0; a<100; a++) {
for(var b = 0; b<100; b++) {
for(var c = 0; c<100; c++) {
if((!a!=!!b^!!-!a||!+!a|!c) == 0) {
console.log(a,b, c);
}
}
}
}
a b c
=====
0 0 1
0 0 2
0 0 3
0 0 4
0 0 5
0 0 6
0 0 7
0 0 8
0 0 9
0 0 10
0 0 11
0 0 12
0 0 13
0 0 14
0 0 15
0 0 16
0 0 17
0 0 18
0 0 19
0 0 20
0 0 21
0 0 22
0 0 23
0 0 24
0 0 25
0 0 26
0 0 27
0 0 28
0 0 29
0 0 30
0 0 31
0 0 32
0 0 33
0 0 34
0 0 35
0 0 36
0 0 37
0 0 38
0 0 39
0 0 40
0 0 41
0 0 42
0 0 43
0 0 44
0 0 45
0 0 46
0 0 47
0 0 48
0 0 49
0 0 50
0 0 51
0 0 52
0 0 53
0 0 54
0 0 55
0 0 56
0 0 57
0 0 58
0 0 59
0 0 60
0 0 61
0 0 62
0 0 63
0 0 64
0 0 65
0 0 66
0 0 67
0 0 68
0 0 69
0 0 70
0 0 71
0 0 72
0 0 73
0 0 74
0 0 75
0 0 76
0 0 77
0 0 78
0 0 79
0 0 80
0 0 81
0 0 82
0 0 83
0 0 84
0 0 85
0 0 86
0 0 87
0 0 88
0 0 89
0 0 90
0 0 91
0 0 92
0 0 93
0 0 94
0 0 95
0 0 96
0 0 97
0 0 98
0 0 99
Try this demo : http://jsfiddle.net/ugfsW/ 试试这个演示: http : //jsfiddle.net/ugfsW/
a=0, b=0, c=1 => Result : 0 a = 0,b = 0,c = 1 =>结果:0
The result is always 0 when you have a=0, b=0 (c is not discriminant). 当a = 0,b = 0 (c不具有判别性)时,结果始终为0 。
I assume that Domain(a) = Domain(b) = Domain(c) 我假设域(a)=域(b)=域(c)
Some logical expressions are tautologies , ie, they're always true. 一些逻辑表达式是重言式 ,即它们总是正确的。 It might be the case that you found one.
可能是你找到了一个。 Try to verify it .
尝试验证它 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.