!a!=!!b^!!-!a||!+!a|!c 可以返回 1 以外的任何值吗?

发布于 2024-11-29 02:58:43 字数 307 浏览 1 评论 0原文

我正在和某人玩 Javascript 游戏,我们很高兴制作可笑和荒谬的表达式,以使我们的输入得到特定的输出。

这个迷人的小家伙

!a!=!!b^!!-!a||!+!a|!c

似乎总是返回1。我试图推理出来,但在失去所有 ! 的踪迹后我放弃了。

abc 是否存在不返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

失与倦" 2024-12-06 02:58:43

简短的回答是,是的。 a = false, b = false, c = true 是一个反例,因为您的方程与 (!!a || !!b || !c) 相同。

长答案:

!a!=!!b^!!-!a||!+!a|!c

简化

(((!a) != (!!b)) ^ (!!(-!a))) || ((!+!a)|!c)

((Boolean(a) == Boolean(b)) ^ (!a)) || (Boolean(a) | !c)

所有 abc 仅作为真/假值处理,结果必须是10 因为 |^ 都将布尔值强制转换为数字。

显然(通过检查 || 的右侧)如果 a 为真或 c 为假,则得到 1

如果a为假而c为真,则有两种可能性,

  1. b为真,在这种情况下^子句为 1,因此永远不会到达 || 的右侧。
  2. 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:

!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 | 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.

If a is falsey and c is truthy, you have two possibilities,

  1. b is truthy in which case the ^ clause is 1 so the right of the || is never reached.
  2. b is falsey, in which case the ^ clause is 0 so the right of the || dominates to produce 0.
书信已泛黄 2024-12-06 02:58:43

怎么样:

var a = undefined, b=undefined, c=!a
alert(!a!=!!b^!!-!a||!+!a|!c)
// Output: 0

现场演示。

How about this:

var a = undefined, b=undefined, c=!a
alert(!a!=!!b^!!-!a||!+!a|!c)
// Output: 0

Live demo.

执手闯天涯 2024-12-06 02:58:43

您是否尝试过多次循环运行它:

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

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
丢了幸福的猪 2024-12-06 02:58:43

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)

探春 2024-12-06 02:58:43

一些逻辑表达式是同义反复,即它们总是正确的。可能你找到了一个。尝试验证它

Some logical expressions are tautologies, i.e., they're always true. It might be the case that you found one. Try to verify it.

晨光如昨 2024-12-06 02:58:43

您应该为此使用模型检查器。它会给你所有输出 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文