!a!=!!b^!!-!a||!+!a|!c 可以返回 1 以外的任何值吗?
我正在和某人玩 Javascript 游戏,我们很高兴制作可笑和荒谬的表达式,以使我们的输入得到特定的输出。
这个迷人的小家伙
!a!=!!b^!!-!a||!+!a|!c
似乎总是返回1
。我试图推理出来,但在失去所有 !
的踪迹后我放弃了。
a
、b
和 c
是否存在不返回 1
的值?如果不是,为什么它总是返回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.
This little charming one
!a!=!!b^!!-!a||!+!a|!c
always seemed to return 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
? If not, why does it always return 1
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
简短的回答是,是的。
a = false, b = false, c = true
是一个反例,因为您的方程与(!!a || !!b || !c)
相同。长答案:
简化
为
所有
a
、b
和c
仅作为真/假值处理,结果必须是1
或0
因为|
和^
都将布尔值强制转换为数字。显然(通过检查
||
的右侧)如果a
为真或c
为假,则得到1
。如果
a
为假而c
为真,则有两种可能性,b
为真,在这种情况下^
子句为1
,因此永远不会到达||
的右侧。b
为 false,在这种情况下,^
子句为0
,因此||
的右侧主导生成<代码>0。Short answer, yes.
a = false, b = false, c = true
is a counter-example because your equation is identical to(!!a || !!b || !c)
.Long answer:
is
which reduces to
so all of
a
,b
andc
are only dealt with as truthy/falsey values and the result must be a1
or0
since|
and^
both coerce booleans to numbers.So obviously (from inspection of the right of the
||
) if eithera
is truthy orc
is falsey, you get1
.If
a
is falsey andc
is truthy, you have two possibilities,b
is truthy in which case the^
clause is1
so the right of the||
is never reached.b
is falsey, in which case the^
clause is0
so the right of the||
dominates to produce0
.怎么样:
现场演示。
How about this:
Live demo.
您是否尝试过多次循环运行它:
Did you even try running it in a few loops:
试试这个演示:http://jsfiddle.net/ugfsW/
a=0, b=0, c= 1 =>结果:0
当您a=0, b=0<时,结果始终为0 /a>(c 没有判别性)。
我假设域(a)=域(b)=域(c)
Try this demo : http://jsfiddle.net/ugfsW/
a=0, b=0, c=1 => Result : 0
The result is always 0 when you have a=0, b=0 (c is not discriminant).
I assume that Domain(a) = Domain(b) = Domain(c)
一些逻辑表达式是同义反复,即它们总是正确的。可能你找到了一个。尝试验证它。
Some logical expressions are tautologies, i.e., they're always true. It might be the case that you found one. Try to verify it.
您应该为此使用模型检查器。它会给你所有输出 0 或 1 的值:-) Spin 是一个非常流行的例如模型检查器。
You should use a model checker for this one. It will give you all the values which will output 0 or 1 :-) Spin is a very popular model checker for example.