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++运算符优先级

  
OperatorDescriptionExampleOverloadable
Group 1 (no associativity)
::Scope resolution operatorClass::age = 2;NO
Group 2
()Function callisdigit('1')YES
()Member initalizationc_tor(int x, int y) : _x(x), _y(y*10){};YES
[]Array accessarray[4] = 2;YES
->Member access from a pointerptr->age = 34;YES
.Member access from an objectobj.age = 34;NO
++Post-incrementfor( int i = 0; i < 10; i++ ) cout << i;YES
--Post-decrementfor( int i = 10; i > 0; i-- ) cout << i;YES
const_castSpecial castconst_cast<type_to>(type_from);NO
dynamic_castSpecial castdynamic_cast<type_to>(type_from);NO
static_castSpecial caststatic_cast<type_to>(type_from);NO
reinterpret_castSpecial castreinterpret_cast<type_to>(type_from);NO
typeidRuntime type informationcout &laquo; typeid(var).name(); 
cout &laquo; typeid(type).name();
NO
Group 3 (right-to-left associativity)
!Logical negationif( !done ) …YES
notAlternate spelling for !
~Bitwise complementflags = ~flags;YES
complAlternate spelling for ~
++Pre-incrementfor( i = 0; i < 10; ++i ) cout << i;YES
--Pre-decrementfor( i = 10; i > 0; --i ) cout << i;YES
-Unary minusint i = -1;YES
+Unary plusint i = +1;YES
*Dereferenceint data = *intPtr;YES
&Address ofint *intPtr = &data;YES
newDynamic memory allocationlong *pVar = new long; 
MyClass *ptr = new MyClass(args);
YES
new []Dynamic memory allocation of arraylong *array = new long[n];YES
deleteDeallocating the memorydelete pVar;YES
delete []Deallocating the memory of arraydelete [] array;YES
(type)Cast to a given typeint i = (int) floatNum;YES
sizeofReturn size of an object or typeint size = sizeof floatNum; 
int size = sizeof(float);
NO
Group 4
->*Member pointer selectorptr->*var = 24;YES
.*Member object selectorobj.*var = 24;NO
Group 5
*Multiplicationint i = 2 * 4;YES
/Divisionfloat f = 10.0 / 3.0;YES
%Modulusint rem = 4 % 3;YES
Group 6
+Additionint i = 2 + 3;YES
-Subtractionint i = 5 - 1;YES
Group 7
<<Bitwise shift leftint flags = 33 << 1;YES
>>Bitwise shift rightint flags = 33 >> 1;YES
Group 8
<Comparison less-thanif( i < 42 ) …YES
<=Comparison less-than-or-equal-toif( i <= 42 ) ...YES
>Comparison greater-thanif( i > 42 ) …YES
>=Comparison greater-than-or-equal-toif( i >= 42 ) ...YES
Group 9
==Comparison equal-toif( i == 42 ) ...YES
eqAlternate spelling for ==
!=Comparison not-equal-toif( i != 42 ) …YES
not_eqAlternate spelling for !=
Group 10
&Bitwise ANDflags = flags & 42;YES
bitandAlternate spelling for &
Group 11
^Bitwise exclusive OR (XOR)flags = flags ^ 42;YES
xorAlternate spelling for ^
Group 12
|Bitwise inclusive (normal) ORflags = flags | 42;YES
bitorAlternate spelling for |
Group 13
&&Logical ANDif( conditionA && conditionB ) …YES
andAlternate spelling for &&
Group 14
||Logical ORif( conditionA || conditionB ) ...YES
orAlternate 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 operatorint a = b;YES
+=Increment and assigna += 3;YES
-=Decrement and assignb -= 4;YES
*=Multiply and assigna *= 5;YES
/=Divide and assigna /= 2;YES
%=Modulo and assigna %= 3;YES
&=Bitwise AND and assignflags &= new_flags;YES
and_eqAlternate spelling for &=
^=Bitwise exclusive or (XOR) and assignflags ^= new_flags;YES
xor_eqAlternate spelling for ^=
|=Bitwise normal OR and assignflags |= new_flags;YES
or_eqAlternate spelling for |=
<<=Bitwise shift left and assignflags <<= 2;YES
>>=Bitwise shift right and assignflags >>= 2;YES
Group 17
throwthrow exceptionthrow EClass(“Message”);NO
Group 18
,Sequential evaluation operatorfor( 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/archive/2012/06/23/2559191.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值