为什么i | = j | = k | =(j + = i) - - (k +++ k) - - (i = + j)== 11? [英] Why does i|= j|= k|= (j+= i) - - (k+++k) - - (i =+j) == 11?

查看:385
本文介绍了为什么i | = j | = k | =(j + = i) - - (k +++ k) - - (i = + j)== 11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我开始研究的项目中遇到了这段代码。最初的开发者不再可用了,我对它没有任何意义。

I came across this code in a project I have started working on. The original developer is no longer available, and I can't make any sense of it.

k = (j = (i = 0) + 2) + 1;
return i|= j|= k|= (j+= i) - - (k+++k) - - (i =+j);

它产生的值 11 。这是如何工作的?

It produces a value of 11. How does this work?

什么是 = + 运算符?

什么是 +++ 运算符?

什么是 - - 运营商?

什么是 | = 运营商?

推荐答案


什么是 = + 运算符?

这是两个运算符,一个赋值运算符, = ,还有一个一元加号, + ,什么都不做。

That's two operators, one assignment operator, =, and one unary plus, +, which does nothing.

你输入错误并且是指复合赋值运算符 + =

Did you typo and mean the compund assignment operator +=?


什么是 +++ 运算符?

还有两个运算符,一个后增量, ++ ,以及另外一个, + ,(根据最大的munch规则,选择最长的有效令牌,如果选择了最短的有效令牌,它将成为一个加法和两个一元加号)。

Also two operators, one post-increment, ++, and one addition, +, (per the maximal munch rule, the longest valid token is chosen, it would become one addition and two unary plus if the shortest valid token were chosen).


什么是 - 运算符?

再两个运算符,一个减法,和一元减号(否定)。

Again two operators, one subtraction, and one unary minus (negation).


什么是 | = 运算符?

复合赋值,bitwise-oring [或者,在 boolean values,logical-oring]带有右侧值的左侧值,并将其存储在左侧变量中。

A compound assignment, bitwise-oring [or, in the case of boolean values, logical-oring] the left-hand-side value with the right-hand-side value and storing that in the left-hand-side variable.

a |= b;

几乎相当于

a = a | b;

但左侧操作数仅评估一次,后者可能需要显式转换前者没有。

but the left-hand-side operand is evaluated only once, and the latter may need an explicit cast where the former doesn't.

k = (j = (i = 0) + 2) + 1;
return i|= j|= k|= (j+= i) - - (k+++k) - - (i =+j);




它产生的值为11.这是如何工作的?

It produces a value of 11. How does this work?

第一行相当于

i = 0;
j = i+2;
k = j+1;

作业( i = 0 例如)计算存储的值(在 i 这里)。

The assignment (i = 0 for example) evaluates to the value stored (in i here).

下一行是,有适当的间距,和隐含括号添加

The next line is, with proper spacing, and implicit parentheses added

return i |= (j |= (k |= (((j += i) - (-(k++ + k))) - (-(i = +j)))));




  • i | = stuff_1 i 被评估(0), stuff_1 被评估,按位或被采用,并将结果存储在 i 中。由于 i 最初为0,相当于 i = stuff_1

    • i |= stuff_1: i is evaluated (0), stuff_1 is evaluated, the bitwise or is taken, and the result stored in i. Since i is originally 0, that is equivalent to i = stuff_1.

      j | = stuff_2 j 评估(2), stuff_2 被评估,按位或被采用,结果存储在 j

      j |= stuff_2: j is evaluated (2), stuff_2 is evaluated, the bitwise or is taken, and the result is stored in j.

      k | = stuff_3 k 评估(3),然后 stuff_3 ,从左到右。

      k |= stuff_3: k is evaluated (3), then stuff_3, left-to-right.


      • (j + = i) i 添加到 j ,将金额存储在中j 并返回 j 的新值。
        由于 i 为0, j 不会改变且值为2.

      • (k ++ + k)取旧值 k (3),增量 k 并添加 k 的新值(4),结果为7.该值被否定,且值为否定值(-7)从2中减去,得到 2 - (-7)= 9

      • (i = + j) j (2)的值存储在 i 并且表达式的值也是2.该值被否定(-2)并从我们从前面的操作中得到的9中减去,因此 stuff_3 的计算结果为11,副作用

      • (j += i) adds i to j, stores the sum in j and returns j's new value. Since i is 0, j doesn't change and the value is 2.
      • (k++ + k) takes the old value of k (3), increments k and adds k's new value (4), resulting in 7. That value is negated, and the negated value (-7) subtracted from 2, resulting in 2 - (-7) = 9.
      • (i = +j) stores the value of j (2) in i and the value of the expression is also 2. The value is negated (-2) and subtracted from the 9 we got from the previous operations, so stuff_3 evaluates to 11, with the side effects that


      • i 的存储值现在为2

      • j 的存储值现在为2(实际上没有变化,因为 i 最初为0)

      • k 的存储值现在为4

      • the stored value of i is now 2
      • the stored value of j is now 2 (didn't actually change, since i was 0 initially)
      • the stored value of k is now 4

      j (2)按位或按 stuff_2 (11)的值,结果为11.该值存储在<$中c $ c> j ,以及 stuff_1 的值( j | = stuff_2 )是11。

      the old value of j (2) is bitwise or'ed with the value of stuff_2 (11), resulting in 11. The value is stored in j, and the value of stuff_1 (j |= stuff_2) is 11.

      的旧值(0)按位或按值 stuff_1 (11),结果记录在 i 中,且值 i | = stuff_1 是11.然后返回该值。

      the old value of i (0) is bitwise or'ed with the value of stuff_1 (11), the result sored in i, and the value of i |= stuff_1 is 11. That value is then returned.

      这篇关于为什么i | = j | = k | =(j + = i) - - (k +++ k) - - (i = + j)== 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆