简体   繁体   English

可以!a!= !! b ^ !! - !a ||!+!a |!c返回1以外的其他内容?

[英]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 ? 是否有abc值不返回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 | 所以abc都只作为truthy / falsey值处理,结果必须是10因为| 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是真的,你有两种可能性,

  1. b is truthy in which case the ^ clause is 1 so the right of the || b是真实的,在这种情况下, ^子句是1因此||的右边 is never reached. 永远不会到达。
  2. 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

Live demo. 现场演示。

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 . 尝试验证它

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. 它将为您提供输出0或1的所有值:-) Spin是一个非常受欢迎的模型检查器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以大于比较返回除true或false之外的任何内容吗? - Can greater than comparison return anything other than true or false? JavaScript中的&#39;hello&#39;==(除“ hello”以外,其他都会返回true的内容)? - 'hello' == (anything that will return true other than 'hello') in JavaScript? 除了xhr请求外,history.pushState还能执行其他操作吗? - Can history.pushState do anything other than an xhr request? 可以在类/函数以外的任何东西上调用 new - Can new be called on anything other than a class/function 如何禁用单击除对话框以外的任何内容 - How to disable clicking on anything other than dialog 来自GetLocations()(来自Google Maps API)的回调可以调用除API函数之外的任何内容吗? - can the callback from GetLocations() (from the Google Maps API) call anything other than API functions? 如何检查字符串是否包含某个字母,后跟除自身或元音以外的任何字母? - How can I check if string contains a certain letter followed by anything other than itself or vowels? 除了警报之外,我还能用什么来告诉用户十六进制代码已被复制? - Is there anything I can use other than alert to tell the user the hex code was copied? 我怎样才能防止在按键上输入字母数字以外的任何内容 - how can i prevent to take input onkeypress anything other than alphanumeric 如何将此递归javascript输出到控制台以外的任何其他内容 - How can i make this recursive javascript output to anything other than the console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM