Swift operator.

An operator is a symbol that tells the compiler to perform a mathematical or logical operation.

SWIFT provides the following operators:

  • Arithmetic operator
  • Comparison operator
  • Logical operator
  • An operator
  • The assignment operator
  • Interval operator
  • Other Operators

In this section we will look at arithmetic, relational, logical, bitwise, assignment, and other operators in detail.


Arithmetic operator

The following table lists the arithmetic operators supported by SWIFT, where variable A is 10 and variable B is 20:

The operator describe The instance
+ A plus sign A plus B is equal to 30
A minus sign A − B results in -10
* Multiplication sign A times B is equal to 200
/ devide B/A is equal to 2
% For more than B % A results in 0

Note:It has been removed from SWIFT3
++,
.

The instance

The following is a simple example of an arithmetic operation:

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("B/A ") \ "(B/A)) + A + = 1 / / similar print (" A + = 1 after A value is \" (A)) = 1 / B/B - similar print (" B - = 1 has A value of B \ "(B))

The execution results of the above procedures are as follows:

A + B = 30 A - B = -10 A * B = 200 B/A = 2 A += 11 B -= 1 B = 19

Comparison operator

The following table lists the comparison operators supported by the Swift language, where variable A is 10 and variable B is 20:

The operator describe The instance
= = Is equal to the (A == B) = false
! = Is not equal to (A ! = B) True.
> Is greater than (A > B) is false.
< Less than (A < B) is true.
> = Greater than or equal to (A >= B) is false.
< = Less than or equal to (A <= B) is true.

The instance

Here is a simple example of the comparison operation:

Import Cocoa var A = 10 var B = 20 print("A == B ") print("A! = B Result: \(A! = B) ") print (" the result is A > B: \ "(A > B)) print (" A < B results: \ print (A < B)") (" the result is: A > = B \ = (A > B) ") print (" A < = B results as follows: \(A <= B)")

The execution results of the above procedures are as follows:

If (A == B) = false A! = B = true A > B = false A BBB = B false A BBB = B false A BBB = B false A BBB = B

Logical operator

The following table lists the logical operators supported by the Swift language, where variables A are true and B are false:

The operator describe The instance
&& Logic and. The operator is TRUE if both sides are TRUE. (A && B) is false.
\ \ The logical or. Operator is TRUE if at least one of both sides is TRUE. (A \ \ B) to true.
! Logic. Boolean values are inverted, making true false, and false true. ! (A && B) is true.

Here is a simple example of a logical operation:

Import the Cocoa var = true var B = false print (" results for A & B: \ "(A & B)) print (" A | | B results as follows: \ [| A | B)") print (! "" A result: \(! A)") print("! B Result: \(! B)")

The execution results of the above procedures are as follows:

Results for A & B: false | A | B results as follows: true! A: false! B: true

An operator

An operator to manipulate bits, ~, &, |, ^ respectively invert, bitwise and with bitwise and, or, with bitwise exclusive or operation, the following table instances:

p q p & q p \ q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

If I specify A = 60; And B = 13; The binary corresponding to the two variables is:

A = 0011 1100

B = 0000 1101

Perform bit operation:

The operator describe The illustration The instance
& And by location. Operates on two numbers, bitwise and, and returns a new number, each of which is 1 only if the same bit of the two input numbers is 1. (A & B) The result is 12, and the binary is 0000 1100
\ Or by location. By the bit or operator \ Compare two numbers and then return a new number whose each bit is set to 1 only if the same bit of the two input numbers is not 0(that is, either one or both). (A \ B) The result is 61, 0011 1101 in binary
^ Exclusive-bitwise. The exclusive-bitwise operator ^ compares two numbers and returns a number whose bits are set to 1 only if the same bit of the two input numbers is different, or 0 if they are the same. (A ^ B) = 49, 0011 0001 in binary
~ The bitwise negation operator ~ negates each bit of an operand. (~A) 1100 0011 in 2’s complement form.
<< Move it bit by bit to the left. The left-shift operator (<<) moves all the bits of the operand to the left by the specified number of bits. The following figure shows the result of 11111111 << 1 (11111111 moved to the left one place). Blue numbers represent moved bits, gray bits represent discarded bits, and empty bits are filled with orange zeros. A << 2 results in 240, binary 1111 0000
>> Move it to the right. The right-shift operator (>>) moves all bits of the operand to the right by the specified number of bits. The following figure shows the result of 11111111 >> 1 (11111111 shifted to the right one place). Blue numbers represent moved bits, gray bits represent discarded bits, and empty bits are filled with orange zeros. A >> 2 results in 15, binary is 0000 1111

The following is a simple example of bitwise operation:

Import the Cocoa var binary for A = 60 / / var B = 13 1100/0011 / binary for 0000 1101 print (" kathi Jones from A&B results for: \ (kathi Jones from A&B) ") print (" A | B results as follows: \ (A | B) ") print (results for A ^ B: \ "(A ^ B)") print (" ~ A result for: \ "(to A))

The execution results of the above procedures are as follows:

Kathi Jones from A&B results as follows: 12 results for A | B: results of A ^ B 61:49 ~ A result for: - 61

The assignment operation

The following table lists the basic assignment operations for the Swift language:

The operator describe The instance
= A simple assignment that specifies that the right-hand operand is assigned to the left-hand operand. C = A + B Assigns the result of A + B to C
+ = The left operand is assigned after the sum of the two operands. C plus theta is equal to A is the same thing as C is equal to C plus A
– = Subtract and assign. Subtract and assign to the left operand. C minus is equal to A is the same thing as C is equal to C minus A
* = Multiply and assign. Multiply and assign the operand to the left. C So theta is equal to A is the same thing as C is equal to C A
/ = Divide and assign. Divide and assign the operand to the left. C /= A is the same thing as C = C/A
% = The left operand is assigned to the left operand after the left operand is mod. C %= A is equivalent to C = C % A
< < = Shift bitwise to the left and assign the value C <<= 2 is the same thing as C = C << 2
> > = Shift bitwise to the right before assigning C times b0 times b1 is equal to 2 is the same thing as C times b2 times b3, 2
& = Assign value after bitwise and operation C&2 is the same thing as C&2
^ = The bitwise XOR operator is followed by the assignment C ^= 2 is the same thing as C = C ^ 2
\ = Assign values bitwise or after operation C \ = 2 equals C = C \ 2

Here is a simple example of an assignment operation:

Import Cocoa var A = 10 var B = 20 var C = 100 C = A + B print("C = = \(C)") C += A print("C += A print ") \ (C) ") C - = A print (" results for C: \ "(C)) * C = A print (" results for C: \ (C)") = C/A print (" C results as follows: \ "(C)) / / the following tests have A comment, comments may be removed test each instance / * C % = A print (" results for C: \ (C)") C < < = A print (" results for C: \ (C) ") C > > = A print (" C results as follows: \ (C) ") & C = A print (" results for C: \ (C) ") C ^ = A print (" results for C: \ (C) ") C | = A print (" results for C: \ "(C)) * /

The execution results of the above procedures are as follows:

C result: 30 C result: 40 C result: 30 C result: 300 C result: 30

Interval operator

SWIFT provides two interval operators.

The operator describe The instance
Closed interval operator The closed interval operator (a… B) Define an interval containing all values from a to b(both a and b), b must be greater than or equal to a. The closed interval operator is useful when iterating over all the values of an interval, as in a for-in loop: 1… The 5 intervals are 1, 2, 3, 4, and 5
Half-open interval operator Half open interval (a.. <b) Defines a range from A to B but not including B. It is called a half-open interval because it contains the first value but not the last. 1.. The < 5 interval values are 1, 2, 3, and 4

Here is a simple example of interval operation:

Import Cocoa print(" Closed interval operator :") for index in 1... 5 {print("\(index) * 5 = \(index * 5)")} print("\(index * 5)") for index in 1.. <5 { print("\(index) * 5 = \(index * 5)") }

The execution results of the above procedures are as follows:

Closed interval operator: 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 5 * 5 = 25 Half open interval operator: 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20

Other Operators

Swift provides other types of operators, such as the meta, binary, and ternary operators.

  • Unary operators operate on a single operand object (e.g-a). Unary operators are prefixed and postfixed, with the prefixed operator immediately preceding the operand (e.g! b), the postfix operator should be immediately after the operation object (for example, c!). .Remark:There is nothing like C in Java/C! The syntax for Swift is used in the Optional type.
  • The binary operator operates on two operand objects (e.g2 + 3), are central because they appear between two action objects.
  • The ternary operator operates on three operands. As in C, Swift has only one ternary operator, the ternary operator (a ? b : c).
The operator describe The instance
One yuan minus Prefix numbers with a – sign – 3 or 4
One yuan to add Prefix the number with a + sign Plus 6 is equal to 6
Ternary operator condition ? X : Y ifconditionIs true if the value is X, otherwise Y

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 results of the above procedures are as follows:

The value of -a is: -1, and the value of A + B is: 3. Ternary operation: 1. Ternary operation: 2

Operator precedence

An expression may contain multiple data objects with different data types that are joined by different operators. Because the expression has a variety of operations, different operation sequences may lead to different results and even wrong operation errors, because when the expression contains a variety of operations, it must be combined in a certain order to ensure the rationality of the operation and the correctness and uniqueness of the result.

Priority decreases from top to bottom, with the top having the highest precedence and the comma operator having the lowest precedence.

In the same priority, it is calculated in combination order. Most operations are evaluated from left to right, but only three precedents are combined from right to left: the unary operator, the conditional operator, and the assignment operator.

Basic priorities need to be remembered:

  • Pointer is optimal, and monocular operation is superior to binocular operation. Such as plus or minus sign.
  • Multiply and divide (modules) first, then add and subtract.
  • Arithmetic first, shift second, bitwise last. Note that 1 << 3 + 2&7 is equivalent to (1 << (3 + 2))&7
  • The logical operation finally evaluates

SWIFT operator priority (from high to low) :

The operator The instance
An operator > > & < < and > > > >
Multiplication operator & % & /
The addition operator \ &+ &- + – ^
Interval operator . <…
Type conversion operator is as
Aggregation of nil ??
Comparison operator ! = > < >= <= === ==
Logic and Operators &&
Logic or operator \ \
Wave arrow ~ >
Ternary operator ? :
Arrow function ( )
The assignment operator \ = %= /= &<<= &>>= &= *= > bb3 = <<= ^= += -=

Here is a simple example of operator precedence:

Import Cocoa var A = 0 A = 2 + 3 * 4% print("A = : \(A)")

The execution results of the above procedures are as follows:

The value of A is: 4

Example analysis:

According to operator priority, the operation of the above program can be resolved as the following steps. The expression is equivalent to:

2 + ((3 * 4) % 5)

(3 * 4) = 12, so the expression is equal to:

2 + (12% 5)

The second step calculates that 12% 5 = 2, so the expression is equivalent to:

2 + 2

At this point you can easily see that the result of the calculation is 4.