Operators Associativity

运算符优先级表

优先级 运算符 结合性
1 () [] . 从左到右
2 ! +(正)  -(负) ~ ++ -- 从右向左
3 * / % 从左向右
4 +(加) -(减) 从左向右
5 << >> >>> 从左向右
6 < <= > >= instanceof 从左向右
7 ==   != 从左向右
8 &(按位与) 从左向右
9 ^ 从左向右
10 | 从左向右
11 && 从左向右
12 || 从左向右
13 ?: 从右向左
14 = += -= *= /= %= &= |= ^=  ~=  <<= >>=   >>>= 从右向左
 
说明:
 
  1、 该表中优先级按照从高到低的顺序书写,也就是优先级为1的优先级最高,优先级14的优先级最低。
  2、 结合性是指运算符结合的顺序,通常都是从左到右。从右向左的运算符最典型的就是负号,例如3+-4,则意义为3加-4,符号首先和运算符右侧的内容结合。
  3、 instanceof作用是判断对象是否为某个类或接口类型,后续有详细介绍。
  4、 注意区分正负号和加减号,以及按位与和逻辑与的区别
  其实在实际的开发中,不需要去记忆运算符的优先级别,也不要刻意的使用运算符的优先级别,对于不清楚优先级的地方使用小括号去进行替代,示例代码:
         int m = 12;
         int n = m << 1 + 2;
         int n = m << (1 + 2); //这样更直观
 
   int r = (2 * l - m + 3 * n) >>1- 128;//首先执行的是1-128,然后在进行>>运算!!,所以要加括号
   int r = ((2 * l - m + 3 * n) >>1)- 128
 

C++运算符优先级

  
Operator Description Example Overloadable
Group 1 (no associativity)
:: Scope resolution operator Class::age = 2; NO
Group 2
() Function call isdigit('1') YES
() Member initalization c_tor(int x, int y) : _x(x), _y(y*10){}; YES
[] Array access array[4] = 2; YES
-> Member access from a pointer ptr->age = 34; YES
. Member access from an object obj.age = 34; NO
++ Post-increment for( int i = 0; i < 10; i++ ) cout << i; YES
-- Post-decrement for( int i = 10; i > 0; i-- ) cout << i; YES
const_cast Special cast const_cast<type_to>(type_from); NO
dynamic_cast Special cast dynamic_cast<type_to>(type_from); NO
static_cast Special cast static_cast<type_to>(type_from); NO
reinterpret_cast Special cast reinterpret_cast<type_to>(type_from); NO
typeid Runtime type information cout &laquo; typeid(var).name(); 
cout &laquo; typeid(type).name();
NO
Group 3 (right-to-left associativity)
! Logical negation if( !done ) … YES
not Alternate spelling for !
~ Bitwise complement flags = ~flags; YES
compl Alternate spelling for ~
++ Pre-increment for( i = 0; i < 10; ++i ) cout << i; YES
-- Pre-decrement for( i = 10; i > 0; --i ) cout << i; YES
- Unary minus int i = -1; YES
+ Unary plus int i = +1; YES
* Dereference int data = *intPtr; YES
& Address of int *intPtr = &data; YES
new Dynamic memory allocation long *pVar = new long; 
MyClass *ptr = new MyClass(args);
YES
new [] Dynamic memory allocation of array long *array = new long[n]; YES
delete Deallocating the memory delete pVar; YES
delete [] Deallocating the memory of array delete [] array; YES
(type) Cast to a given type int i = (int) floatNum; YES
sizeof Return size of an object or type int size = sizeof floatNum; 
int size = sizeof(float);
NO
Group 4
->* Member pointer selector ptr->*var = 24; YES
.* Member object selector obj.*var = 24; NO
Group 5
* Multiplication int i = 2 * 4; YES
/ Division float f = 10.0 / 3.0; YES
% Modulus int rem = 4 % 3; YES
Group 6
+ Addition int i = 2 + 3; YES
- Subtraction int i = 5 - 1; YES
Group 7
<< Bitwise shift left int flags = 33 << 1; YES
>> Bitwise shift right int flags = 33 >> 1; YES
Group 8
< Comparison less-than if( i < 42 ) … YES
<= Comparison less-than-or-equal-to if( i <= 42 ) ... YES
> Comparison greater-than if( i > 42 ) … YES
>= Comparison greater-than-or-equal-to if( i >= 42 ) ... YES
Group 9
== Comparison equal-to if( i == 42 ) ... YES
eq Alternate spelling for ==
!= Comparison not-equal-to if( i != 42 ) … YES
not_eq Alternate spelling for !=
Group 10
& Bitwise AND flags = flags & 42; YES
bitand Alternate spelling for &
Group 11
^ Bitwise exclusive OR (XOR) flags = flags ^ 42; YES
xor Alternate spelling for ^
Group 12
| Bitwise inclusive (normal) OR flags = flags | 42; YES
bitor Alternate spelling for |
Group 13
&& Logical AND if( conditionA && conditionB ) … YES
and Alternate spelling for &&
Group 14
|| Logical OR if( conditionA || conditionB ) ... YES
or Alternate spelling for ||
Group 15 (right-to-left associativity)
? : Ternary conditional (if-then-else) int i = (a > b) ? a : b; NO
Group 16 (right-to-left associativity)
= Assignment operator int a = b; YES
+= Increment and assign a += 3; YES
-= Decrement and assign b -= 4; YES
*= Multiply and assign a *= 5; YES
/= Divide and assign a /= 2; YES
%= Modulo and assign a %= 3; YES
&= Bitwise AND and assign flags &= new_flags; YES
and_eq Alternate spelling for &=
^= Bitwise exclusive or (XOR) and assign flags ^= new_flags; YES
xor_eq Alternate spelling for ^=
|= Bitwise normal OR and assign flags |= new_flags; YES
or_eq Alternate spelling for |=
<<= Bitwise shift left and assign flags <<= 2; YES
>>= Bitwise shift right and assign flags >>= 2; YES
Group 17
throw throw exception throw EClass(“Message”); NO
Group 18
, Sequential evaluation operator for( i = 0, j = 0; i < 10; i++, j++ ) … YES

C#中的优先级

 

优先级 类别 运算符
1 基本 (x) x.y f(x) a[x] x++ x――new typeof sizeof checked unchecked
2 单目 + - ! ~ ++x ――x (T)x
3 乘法与除法 * / %
4 加法与减法 + -
5 移位运算 << >>
6 关系运算 ﹤ > <= >= is
7 条件等 = = ! =
8 位逻辑与 &
9 位逻辑异或 ^
10 位逻辑或 |
11 条件与 &&
12 条件或
13 条件 ?:
14 赋值 = *= /= %= += -= <<= >>= &= ^= |=

原文地址:https://www.cnblogs.com/qiengo/p/2559191.html