简体   繁体   中英

x<<y>>z order of evaluation in C

What is the order of evaluation in C in the case of x<<y>>z ? Is it (x<<y)>>z , because of the Left to Right associativity ?

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)

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

For more references.. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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