为什么答案是42? [英] Why is the answer 42?
问题描述
我试图理解这个回文表达式是如何给出的。我知道运算符优先级规则,但这超出了我当前的Javascript级别。我该如何开始?
I'm trying to understand how this palindrome expression is giving 42. I know about operator precedence rules, but this is beyond my current Javascript level. How can I start?
alert ("The answer is " +
[(0>>(0==0))+([0]+[(0==0)+(0==0)]^0)]*[(0^[(0==0)+(0==0)]+[0])+((0==0)<<0)]
);
推荐答案
基本要素如下:
0==0
这是 true
,可以强制转换为 1
。
This is true
, which can be coerced in to 1
.
a >> b
右移运营商。在这种情况下,它仅在表达式的开头用作 0>> 1
评估为 0
。
The right-shift operator. In this case, it's only used at the beginning of the expression as 0 >> 1
which evaluates to 0
.
a^b
按位异或。上述两种用法都有 a
或 b
0
,因此结果是非零操作数,强制转换为整数。
Bitwise XOR. Both usages above have either a
or b
are 0
, and so the result is the non-zero operand, coerced into an integer.
[a] + [b]
添加 a
和的字符串b
,评估为ab
;如果 a
和 b
都是数字(例如 [0] + [1]
结果可以强制转换为数字。
String addition of a
and b
, evaluates to "ab"
; if both a
and b
are numeric (e.g. [0]+[1]
the result can be coerced into a numeric.
[a] * [b]
显然可以在单个元素数组上执行乘法。这相当于 a * b
。
Multiplication can be performed on single element arrays, apparently. So this is equivalent to a*b
.
最后,
a << b
左移运算符;对于正整数,这有效地乘以2到幂 b
。在上面的表达式中,它与 b = 0
一起使用,因此结果为 a
,强制转换为整数。
The left-shift operator; for positive integers this effectively multiplies by 2 to the power of b
. In the expression above, this is used with b = 0
, so the result is a
, coerced into an integer.
如果你应用了正确的操作顺序,你就会得到 [ 2] * [21]
评估为 42
。
If you apply the correct order of operations, you get out [2] * [21]
which evaluates to 42
.
这篇关于为什么答案是42?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!