运算符
运算符是一个符号,它告诉编译器执行特定的数学或逻辑操作。Objective-C语言有丰富的内置运算符并提供了以下几种类型:
- 算术运算符
- 关系运算符
- 逻辑运算符
- 位运算符
- 赋值运算符
- 其它运算符
算术运算符
表2.3列出了所有支持Objective-C语言的算术运算符。假设变量A=10,变量B=20,则:
运算符 | 描述 | 示例 |
---|---|---|
+ | adds two operands | A + B will give 30 |
- | subtracts second operands from the first | A - B will give -10 |
* | multiplies both operands | A * B will give 200 |
/ | divides numerator by denominator | B / A will give 2 |
% | modulus operator and remainder of after an integer division | B % A will give 0 |
++ | Increment operator increases integer value by one | A ++ will give 11 |
-- | decrement operator decreases integer value by one | A -- will give 9 |
关系运算符
表2.4列出了所有支持Objective-C语言的关系运算符。假设变量A=10,变量B=20,则:
运算符 | 描述 | 示例 |
---|---|---|
== | checks if the values of two operands are equal or not; if yes, then condition becomes true | (A == B)is not true |
!= | checks if the values of two operands are equal or not; if values are not equal, then condition becomes true | (A != B)is true |
> | checks if the values of left operands is greater than value of right operand; if yes, then codition becomes true | (A > B)is not true |
< | checks if the values of left operands is less than value of right operand; if yes, then codition becomes true | (A < B)is true |
>= | checks if the values of left operands is greater than or equal to the value of right operand; if yes, then codition becomes true | (A >= B)is not true |
<= | checks if the values of left operands is less than or equal to the value of right operand; if yes, then codition becomes true | (A <= B)is true |
逻辑运算符
表2.5列出了所有支持Objective-C语言的逻辑运算符。假设变量A=1,变量B=0,则:
运算符 | 描述 | 示例 |
---|---|---|
&& | called Logical AND Operator. If both the operands are non zero then condition becomes true | (A && B)is false |
││ | called Logical OR Operator. If any of the two operands is non zero then condition becomes true | (A││B)is true |
! | called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false | (A ! B)is true |
位运算符
表2.6列出了所有支持Objective-C语言的位运算符。假设变量A=60,变量B=13,二进制为A=0011 1100,B=0000 1101,则:
运算符 | 描述 | 示例 |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B)will give 12, which is 0000 1100 |
│ | Binary OR Operator copies a bit if it exists in either operands. | (A │ B)will give 61, which is 0011 1101 |
^ | Binary XOR Operator copies the bit if it set in one operand but not both. | (A ^ B)will give 49, which is 0011 0001 |
~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~ A)will give -61, which is 1100 0011 in 2's complement form |
<< | Binary Left Shift Operator. The left operands value is moved left by number of bits specified by the right operand. | (A << 2)will give 240, which is 1111 0000 |
>> | Binary Right Shift OPerator. The left operands value is moved right by the number of bits specified by the right operand. | (A >> 2)will give 15, which is 0000 1111 |
赋值运算符
表2.7列出了Objective-C语言所有支持的赋值运算符:
运算符 | 描述 | 示例 |
---|---|---|
= | Simple assignment operator, assigns values from right side operands to left side operand | C = A + B wll assign value of A + B into C |
+= | Add AND assignment operator, it adds right operand to the left operand and assigns the result to left operand | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator, it subtract right operand from the left operand and assigns the result to left operand | C -= A is equivalent to C = C - A |
*= | Multipy AND assignment operator, it multipies right operand with the left operand and assigns the result to left operand | C = A is equivalent to C = C A |
/= | Divide AND assignment operator, it divides left operand with the right operand and assigns the result to left operand | C /= A is equivalent to C = C / A |
%= | Modulus And assignment operator, it takes modulus using two operands and assigns the result to left operand | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator | C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator | C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator | C &= 2 is same as C = C & 2 |
^= | Bitwise exclusive OR and assignment operator | C ^= 2 is same as C = C ^ 2 |
l= | Bitwise inclusive OR and assignment operator | C l= 2 is same as C = C l 2 |
其它运算符
表2.8列出了一些其它重要的运算符,包括sizeof和?:运算符:
运算符 | 描述 | 示例 |
---|---|---|
sizeof() | return the size of an variable | sizeof(a), where a is integer, will return 4 |
& | return the address of an variable | &a; will give actual address of the address |
* | yiibaier to variable | *a; will yiibaier to a variable |
? : | conditional expreeion | If condition is true ? Then value X : otherwise value Y |
运算符优先级
表2.9整理了运算符的优先级。在这里,运算符具有最高优先级则出现在上面的表中,那些低优先级的则出现在表的底部。在一个表达式中,优先级较高的运算符将首先计算。
优先级 | 运算符 | 名称或含义 | 使用形式 | 结合方向 | 说明 |
---|---|---|---|---|---|
1 | [] | 数组下标 | 数组名[常量表达式] | 左到右 | —— |
- | () | 圆括号 | (表达式)/函数名(形参表) | 左到右 | —— |
- | . | 成员选择(对象) | 对象.成员名 | 左到右 | —— |
- | -> | 成员选择(指针) | 对象指针->成员名 | 左到右 | —— |
2 | - | 负号运算符 | -表达式 | 右到左 | 单目运算符 |
- | (类型) | 强制类型转换 | (数据类型)表达式 | 右到左 | —— |
- | ++ | 自增运算符 | ++变量名/变量名++ | 右到左 | 单目运算符 |
- | -- | 自减运算符 | --变量名/变量名-- | 右到左 | 单目运算符 |
- | * | 取值运算符 | *指针变量 | 右到左 | 单目运算符 |
- | & | 取地址运算符 | &变量名 | 右到左 | 单目运算符 |
- | ! | 逻辑非运算符 | !表达式 | 右到左 | 单目运算符 |
- | ~ | 按位取反运算符 | ~表达式 | 右到左 | 单目运算符 |
- | sizeof | 长度运算符 | sizeof(表达式) | 右到左 | —— |
3 | / | 除 | 表达式/表达式 | 左到右 | 双目运算符 |
- | * | 乘 | 表达式*表达式 | 左到右 | 双目运算符 |
- | % | 余数(取模) | 整型表达式/整型表达式 | 左到右 | 双目运算符 |
4 | + | 加 | 表达式+表达式 | 左到右 | 双目运算符 |
- | - | 减 | 表达式-表达式 | 左到右 | 双目运算符 |
5 | << | 左移 | 变量<<表达式 | 左到右 | 双目运算符 |
- | >> | 右移 | 变量>>表达式 | 左到右 | 双目运算符 |
6 | > | 大于 | 表达式>表达式 | 左到右 | 双目运算符 |
- | >= | 大于等于 | 表达式>=表达式 | 左到右 | 双目运算符 |
- | < | 小于 | 表达式<表达式 | 左到右 | 双目运算符 |
- | <= | 小于等于 | 表达式<=表达式 | 左到右 | 双目运算符 |
7 | == | 等于 | 表达式==表达式 | 左到右 | 双目运算符 |
- | ! = | 不等于 | 表达式 ! = 表达式 | 左到右 | 双目运算符 |
8 | & | 按位与 | 表达式&表达式 | 左到右 | 双目运算符 |
9 | ^ | 按位异或 | 表达式^表达式 | 左到右 | 双目运算符 |
10 | l | 按位或 | 表达式l表达式 | 左到右 | 双目运算符 |
11 | && | 逻辑与 | 表达式&&表达式 | 左到右 | 双目运算符 |
12 | ll | 逻辑或 | 表达式ll表达式 | 左到右 | 双目运算符 |
13 | ?: | 条件运算符 | 表达式1? 表达式2: 表达式3 | 右到左 | 三目运算符 |
14 | = | 赋值运算符 | 变量=表达式 | 右到左 | —— |
- | /= | 除后赋值 | 变量/=表达式 | 右到左 | —— |
- | *= | 乘后赋值 | 变量*=表达式 | 右到左 | —— |
- | %= | 取模后赋值 | 变量%=表达式 | 右到左 | —— |
- | += | 加后赋值 | 变量+=表达式 | 右到左 | —— |
- | -= | 减后赋值 | 变量-=表达式 | 右到左 | —— |
- | <<= | 左移后赋值 | 变量<<=表达式 | 右到左 | —— |
- | >>= | 右移后赋值 | 变量>>=表达式 | 右到左 | —— |
- | &= | 按位与后赋值 | 变量&=表达式 | 右到左 | —— |
- | ^= | 按位异或后赋值 | 变量^=表达式 | 右到左 | —— |
- | l= | 按位或后赋值 | 变量l= 表达式 | 右到左 | —— |
15 | , | 逗号运算符 | 表达式,表达式,… | 左到右 | 从左向右顺序运算 |
说明:除了赋值运算符和单目运算符以及条件运算符外(?: )外,所有的运算符都是向左关联的。优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。相同优先级中,按结合顺序计算。基本的优先级需要记住:
- 指针最优,单目运算优于双目运算。如正负号。
- 先乘除(模),后加减。
- 先算术运算,后移位运算,最后位运算。请特别注意:1 << 3 + 2 & 7等价于 (1 << (3 + 2))&7.
- 逻辑运算最后计算。
表达式
表达式,是由数字、算符、数字分组符号(括号)、*变量和约束变量等以能求得数值的有意义排列方法所得的组合。如:算术表达式、逻辑表达式、关系表达式、赋值表达式、逗号表达式等等。
参考: 1. Objective-C 学习笔记 - 第2章 数据类型、运算符和表达式 http://www.jianshu.com/p/88edda182683
2. OC中的数据类型和运算符 http://blog.csdn.net/tangjun201/article/details/45563125