天下

记录修行的印记

C++运算符及优先级

Bitwise Operators ( &, |, ^, ~, <<, >> )


Bitwise operators modify variables considering the bit patterns that represent the values they store.

operatorasm equivalentdescription
& AND Bitwise AND  位与
| OR Bitwise Inclusive OR 位或
^ XOR Bitwise Exclusive OR
异或,即
参与运算的位不同时其结果是1,否则结果为0 。即“相同为0,不同为1”

~ NOT Unary complement (bit inversion) 取反
<< SHL Shift Left 左移
>> SHR Shift Right 右移

从右开始为0位: 
7 6 5 4 3 2 1 0
位与:将某位置为0
位或:将某位置为1

4的二进制:0b
'100'

将x的2位置为0,其它位保留不变.
int x=7;    //0b'111'
&=~4;        //结果:0b'011'

位或:
将x的第2位置为1,其它位保留不变.
int x= 9;    //0b'1001'
|= 4;        //0b'1101'       

There is an established order with the priority of each operator, the priority order is as follows: 

LevelOperatorDescriptionGrouping
1 :: scope Left-to-right
2 () [] . -> ++ --
dynamic_cast static_cast reinterpret_cast const_cast typeid
postfix Left-to-right
3 ++ -- ~ ! sizeof new delete unary (prefix) Right-to-left
* & indirection and reference (pointers)
+ - unary sign operator
4 (type) type casting Right-to-left
5 .* ->* pointer-to-member Left-to-right
6 * / % multiplicative Left-to-right
7 + - additive Left-to-right
8 << >> shift Left-to-right
9 < > <= >= relational Left-to-right
10 == != equality Left-to-right
11 & bitwise AND Left-to-right
12 ^ bitwise XOR Left-to-right
13 | bitwise OR Left-to-right
14 && logical AND Left-to-right
15 || logical OR Left-to-right
16 ?: conditional Right-to-left
17 = *= /= %= += -= >>= <<= &= ^= |= assignment Right-to-left
18 , comma Left-to-right

优先级 运算符 含义 参与运算对象的数目 结合方向
 1 ( )
[ ]
->
.
圆括号运算符
下标运算符
指向结构体成员运算符
结构体成员运算符

双目运算符
双目运算符
双目运算符
自左至右
2
~
++
--
-
(类型)
*
&
sizeof
逻辑非运算符
按位取反运算符
自增运算符
自减运算符
负号运算符
类型转换运算符
指针运算符
取地址运算符
求类型长度运算符
单目运算符 自右至左
3 *
/
%
乘法运算符
除法运算符
求余运算符
双目运算符 自左至右
4 +
加法运算符
减法运算符
双目运算符 自左至右
5 <<
>>
左移运算符
右移运算符
双目运算符 自左至右
6 <
<=
>
>=
关系运算符 双目运算符 自左至右
7 ==
!=
判等运算符
判不等运算符
双目运算符 自左至右
8 &
按位与运算符 双目运算符 自左至右
9 ^
按位异或运算符 双目运算符 自左至右
10 |
按位或运算符 双目运算符 自左至右
11 &&
逻辑与运算符 双目运算符 自左至右
12 || 逻辑或运算符 双目运算符 自左至右
13 ?: 条件运算符 三目运算符 自右至左
14
+=
-=
*=
/=
%=
>>=
<<=
&=
^=
|=
赋值运算符 双目运算符 自右至左
15 , 逗号运算符
(顺序求值运算符)
 
自左至右

posted on 2010-11-18 14:20 天下 阅读(375) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年11月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论