运算符是一个符号,它告诉编译器执行特定的数学或逻辑操作。Objective-C语言有丰富的内置运算符并提供了以下几种类型:
算术运算符关系运算符逻辑运算符位运算符赋值运算符其它运算符表2.3列出了所有支持Objective-C语言的算术运算符。假设变量A=10,变量B=20,则:
运算符描述示例+adds two operandsA + B will give 30-subtracts second operands from the firstA - B will give -10*multiplies both operandsA * B will give 200/divides numerator by denominatorB / A will give 2%modulus operator and remainder of after an integer divisionB % A will give 0++Increment operator increases integer value by oneA ++ will give 11--decrement operator decreases integer value by oneA -- 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 operandC = 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 operandC += 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 operandC -= 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 operandC = 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 operandC /= A is equivalent to C = C / A%=Modulus And assignment operator, it takes modulus using two operands and assigns the result to left operandC %= A is equivalent to C = C % A<<=Left shift AND assignment operatorC <<= 2 is same as C = C << 2>>=Right shift AND assignment operatorC >>= 2 is same as C = C >> 2&=Bitwise AND assignment operatorC &= 2 is same as C = C & 2^=Bitwise exclusive OR and assignment operatorC ^= 2 is same as C = C ^ 2l=Bitwise inclusive OR and assignment operatorC l= 2 is same as C = C l 2表2.8列出了一些其它重要的运算符,包括sizeof和?:运算符:
运算符描述示例sizeof()return the size of an variablesizeof(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 expreeionIf condition is true ? Then value X : otherwise value Y表2.9整理了运算符的优先级。在这里,运算符具有最高优先级则出现在上面的表中,那些低优先级的则出现在表的底部。在一个表达式中,优先级较高的运算符将首先计算。
优先级运算符名称或含义使用形式结合方向说明1[]数组下标数组名[常量表达式]左到右——-()圆括号(表达式)/函数名(形参表)左到右——-.成员选择(对象)对象.成员名左到右——-->成员选择(指针)对象指针->成员名左到右——2-负号运算符-表达式右到左单目运算符-(类型)强制类型转换(数据类型)表达式右到左——-++自增运算符++变量名/变量名++右到左单目运算符---自减运算符--变量名/变量名--右到左单目运算符-*取值运算符*指针变量右到左单目运算符-&取地址运算符&变量名右到左单目运算符-!逻辑非运算符!表达式右到左单目运算符-~按位取反运算符~表达式右到左单目运算符-sizeof长度运算符sizeof(表达式)右到左——3/除表达式/表达式左到右双目运算符-*乘表达式*表达式左到右双目运算符-%余数(取模)整型表达式/整型表达式左到右双目运算符4+加表达式+表达式左到右双目运算符--减表达式-表达式左到右双目运算符5<<左移变量<<表达式左到右双目运算符->>右移变量>>表达式左到右双目运算符6>大于表达式>表达式左到右双目运算符->=大于等于表达式>=表达式左到右双目运算符-<小于表达式<表达式左到右双目运算符-<=小于等于表达式<=表达式左到右双目运算符7==等于表达式==表达式左到右双目运算符-! =不等于表达式 ! = 表达式左到右双目运算符8&按位与表达式&表达式左到右双目运算符9^按位异或表达式^表达式左到右双目运算符10l按位或表达式l表达式左到右双目运算符11&&逻辑与表达式&&表达式左到右双目运算符12ll逻辑或表达式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