简体   繁体   English

X &lt; <y> &gt; z中的评估顺序

[英]x<<y>>z order of evaluation in C

What is the order of evaluation in C in the case of x<<y>>z ? x<<y>>z的情况下,C中的求值顺序是多少? Is it (x<<y)>>z , because of the Left to Right associativity ? (x<<y)>>z ,因为从左到右的相关性?

EDIT Need to know what the standards tell about it, and not guess what's going on by inspection for a particular compiler. 编辑需要知道标准告诉它的内容,而不是通过检查特定编译器来猜测是什么。

是的, >><<是左关联的并具有相同的优先级,因此x << y >> z等同于(x << y) >> z

Online C 2011 Draft Standard (N1570) 在线C 2011标准草案(N1570)

6.5.7 Bitwise shift operators

Syntax

1     shift-expression:
          additive-expression
          shift-expression << additive-expression
          shift-expression >> additive-expression

The syntax indicates both operators are left-associative, as follows: 语法表示两个运算符都是左关联的,如下所示:

x      <<       y         >>           z
    |               |         |            |
    +------ + ------+         |            |
            |                 |            |
            V                 |            V
      shift-expression        >>   additive-expression

是的,你是对的,因为<<和>>运算符具有相同的优先级并且是左关联的

Both << and >> are on same level and their direction is left to right. <<>>都在同一水平,它们的方向是从左到右。

so it will be (x<<y)>>z 所以它将是(x<<y)>>z

For more references.. http://msdn.microsoft.com/en-us/library/2bxt6kc4%28v=vs.71%29.aspx 有关更多参考资料.. http://msdn.microsoft.com/en-us/library/2bxt6kc4%28v=vs.71%29.aspx

Yep it is, but i think it's more secure to do in 2 steps, like x<<y then y>>z cause the compiler can interpret badly a x<<y>>z . 是的,但是我认为通过两个步骤来做更安全,比如x<<y然后y>>z因为编译器可以很难解释x<<y>>z I haven't used bitwise operations since a whole time but if i remember well it's what i said. 我一直没有使用按位操作,但如果我记得很清楚,那就是我所说的。 I hope i've helped you. 我希望我帮助过你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM