Swift operators
Swift Operator
An operator is a symbol used to tell the compiler to perform a mathematical or logical operation.
Swift provides the following operators:
Arithmetic operators
Comparison operators
Logical operators
Bitwise operators
Assignment operators
Interval operator
Other operators
In this chapter we will introduce arithmetic operators, relational operators, and logical operations in detail operators, bitwise operators, assignment operators, and other operators.
Arithmetic operators
The following table lists the arithmetic operators supported by the Swift language, where variable A is 10 and variable B is 20:
##/Division sign B / A The result is 2%Find remainderB % A The result is 0++incrementA++ The result is 11--decrementA --The result is 9Operator | Description | Instance |
---|---|---|
+ | Plus sign | A + B The result is 30 |
− | Minus sign | A − B The result is -10 |
* | Multiplication sign | A * B The result is 200 |
import Cocoa var A = 10 var B = 20 print("A + B 结果为:\(A + B)") print("A - B 结果为:\(A - B)") print("A * B 结果为:\(A * B)") print("B / A 结果为:\(B / A)") A++ print("A++ 后 A 的值为 \(A)") B-- print("B-- 后 B 的值为 \(B)")The execution result of the above program is:
A + B 结果为:30 A - B 结果为:-10 A * B 结果为:200 B / A 结果为:2 A++ 后 A 的值为 11 B-- 后 B 的值为 19
Comparison operatorsThe following table lists the comparison operators supported by the Swift language, where variable A is 10 and variable B is 20: OperatorDescriptionInstance==Equals( A == B) is false. != is not equal to (A != B) is true. > is greater than (A > B) is false. < is less than (A < B) is true. >= is greater than or equal to (A >= B) is false. <= is less than or equal to (A <= B) is true.
import Cocoa var A = 10 var B = 20 print("A == B 结果为:\(A == B)") print("A != B 结果为:\(A != B)") print("A > B 结果为:\(A > B)") print("A < B 结果为:\(A < B)") print("A >= B 结果为:\(A >= B)") print("A <= B 结果为:\(A <= B)")The execution result of the above program is:
A == B 结果为:false A != B 结果为:true A > B 结果为:false A < B 结果为:true A >= B 结果为:false A <= B 结果为:true
Logical operatorsThe following table lists the logical operators supported by the Swift language, where variable A is true and variable B is false: OperatorDescriptionInstance&&Logical AND. TRUE if both sides of the operator are TRUE. (A && B) is false. ||Logical OR. TRUE if at least one of the two sides of the operator is TRUE. (A || B) is true. !Logical negation. Negating a Boolean value makes true become false and false becomes true. !(A && B) is true.
The following is a simple example of logical operation:
import Cocoa var A = true var B = false print("A && B 结果为:\(A && B)") print("A || B 结果为:\(A || B)") print("!A 结果为:\(!A)") print("!B 结果为:\(!B)")
The execution result of the above program is:
A && B 结果为:false A || B 结果为:true !A 结果为:false !B 结果为:true
Bit operators
Bit operators are used to perform binary operations Bitwise operations, ~,&,|,^ are respectively negation, bitwise AND, bitwise AND, bitwise AND and XOR operations, as shown in the following table for examples:
0 | 0 | 0 | 0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1 | 1 | 0 | 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
0 | 1 | 1 | If you specify A = 60; and B = 13;, the binary corresponding to the two variables is: A = 0011 1100 B = 0000 1101 Perform bit operation: ##~ Take bitwise The negation operator ~ negates each bit of an operand. (~A ) The result is -61, which in binary is 1100 0011 in 2's complement form.##<<The following figure shows the result of 11111111 << 1 (11111111 shifted one bit to the left). Blue numbers represent shifted bits, gray represents discarded bits, and empty bits are filled with orange 0s. >>The following figure shows the result of 11111111 >> 1 (11111111 shifted one position to the right). Blue numbers represent shifted bits, gray represents discarded bits, and empty bits are filled with orange 0s.
The following is a simple example of bit operation: import Cocoa var A = 60 // 二进制为 0011 1100 var B = 13 // 二进制为 0000 1101 print("A&B 结果为:\(A&B)") print("A|B 结果为:\(A|B)") print("A^B 结果为:\(A^B)") print("~A 结果为:\(~A)") The execution result of the above program is: A&B 结果为:12 A|B 结果为:61 A^B 结果为:49 ~A 结果为:-61 Assignment operationThe following table lists the basic assignment operations of Swift language: ##%=Find the remainder and then assign the value. Find the remainder of the left and right operands. Then assign the value to the operand on the left. C %= A is equivalent to C = C % A<<=Shift left bitwise and then assign valueC <<= 2 is equivalent to C = C << 2>>= Shift right bitwise and then assign the value C >>= 2 is equivalent to C = C >> 2##&=^=|=The following is a simple example of assignment operation:
import Cocoa var A = 10 var B = 20 var C = 100 C = A + B print("C 结果为:\(C)") C += A print("C 结果为:\(C)") C -= A print("C 结果为:\(C)") C *= A print("C 结果为:\(C)") C /= A print("C 结果为:\(C)") //以下测试已注释,可去掉注释测试每个实例 /* C %= A print("C 结果为:\(C)") C <<= A print("C 结果为:\(C)") C >>= A print("C 结果为:\(C)") C &= A print("C 结果为:\(C)") C ^= A print("C 结果为:\(C)") C |= A print("C 结果为:\(C)") */ The execution result of the above program is: C 结果为:30 C 结果为:40 C 结果为:30 C 结果为:300 C 结果为:30Interval Operator Swift provides two interval operators.OperatorClosed interval operatorHalf-open interval operatorThe following is a simple example of interval operation: import Cocoa print("闭区间运算符:") for index in 1...5 { print("\(index) * 5 = \(index * 5)") } print("半开区间运算符:") for index in 1..<5 { print("\(index) * 5 = \(index * 5)") } The execution result of the above program is: 闭区间运算符: 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 半开区间运算符: 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 Other operatorsSwift provides other types of operators, Such as unary, binary and ternary operators.
The following are simple examples of unary, binary, and ternary operations: import Cocoa var A = 1 var B = 2 var C = true var D = false print("-A 的值为:\(-A)") print("A + B 的值为:\(A + B)") print("三元运算:\(C ? A : B )") print("三元运算:\(D ? A : B )") The execution result of the above program is: -A 的值为:-1 A + B 的值为:3 三元运算:1 三元运算:2 Operator priorityAn expression may contain multiple data objects of different data types connected by different operators; since the expression has multiple operations, different operation orders may produce different results or even incorrect operation errors, because When an expression contains multiple operations, they must be combined in a certain order to ensure the rationality of the operations and the correctness and uniqueness of the results. The priorities decrease from top to bottom, with the top one having the highest priority and the comma operator having the lowest priority. In the same priority, the calculation is based on the order of combination. Most operations are calculated from left to right, and only three precedence levels are combined from right to left. They are unary operators, conditional operators, and assignment operators. Basic priorities need to be remembered:
The following is a simple example of operator precedence: import Cocoa var A = 0 A = 2 + 3 * 4 % 5 print("A 的值为:\(A)") The execution result of the above program For: A 的值为:4 Instance analysis: According to the operator priority, the operation of the above program can be parsed into the following steps, and the expression is equivalent to: 2 + ((3 * 4) % 5) The first step is to calculate: (3 * 4) = 12, so the expression is equivalent to: 2 + (12 % 5) The second step is to calculate 12 % 5 = 2, so the expression is equivalent to: 2 + 2 At this point it can be easily seen that the calculated result is 4. |