Swift operator


Release date:2023-11-24 Update date:2023-12-11 Editor:admin View counts:123

Label:

Swift operator

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

Swift provides the following operators:

  • Arithmetic operator

  • Comparison operator

  • Logical operator

  • Bit operator

  • Assignment operator

  • Interval operator

  • Other operators

In this section, we will introduce you in detail about arithmetic operators,relational operators, logical operators, bit operators, assignment operators, and other operators.

Arithmetic operator

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

Operator

Description

Example

Plus sign

A + B result is 30

Minus sign

A − B result is-10

*

Multiplication sign

A*B result is 200.

/

Division sign

B / A result is 2.

%

Beg for surplus

B% A result is 0

Note: + +,– have been removed from swift3.

Example

Here is a simple example of an arithmetic operation:

import Cocoa

var A = 10
var B = 20

print("A + B the result is:\(A + B)")
print("A - B the result is:\(A - B)")
print("A * B the result is:\(A * B)")
print("B / A the result is:\(B / A)")
A += 1   // same as A++
print("A += 1 ,the value of A is \(A)")
B -= 1   // same as  B--
print("B -= 1 ,the value of B is \(B)")

The execution results of the above procedures are as follows:

The result of A + B is :30
The result of A - B is:-10
The result of A * B is:200
The result of B / A is:2
A += 1 , the value of A is 11
B -= 1 , the value of A is 19

Comparison operator

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

Operator

Description

Example

==

Equal to

(a = = B) is false.

! =

Not equal to

(a! = B) is true.

>

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.

Example

The following is a simple example of a comparison operation:

import Cocoa

var A = 10
var B = 20

print("A == B the result is:\(A == B)")
print("A != B the result is:\(A != B)")
print("A > B the result is:\(A > B)")
print("A < B the result is:\(A < B)")
print("A >= B the result is:\(A >= B)")
print("A <= B the result is:\(A <= B)")

The execution results of the above procedures are as follows:

A == B the result is:false
A != B the result is:true
A > B the result is:false
A < B the result is:true
A >= B the result is:false
A <= B the result is:true

Logical operator

The following table lists the logical operators supported by the Swift language, where the variable A is true variable B is false :

Operator

Description

Example

&&

Logic and. TRUE if both sides of the operator are TRUE.

(a & B) is false.

||

Logic or. TRUE if at least one of the sides of the operator is TRUE.

(a | | B) is true.

!

Logic is not. The Boolean value is reversed, making the true change from false,false to true.

! (a & B) is true.

Here is a simple example of a logical operation:

import Cocoa

var A = true
var B = false

print("A && B the result is:\(A && B)")
print("A || B the result is:\(A || B)")
print("!A the result is:\(!A)")
print("!B the result is:\(!B)")

The execution results of the above procedures are as follows:

A && B the result is:false
A || B the result is:true
!A the result is:false
!B the result is:true

Bit operator

The bit operator is used to operate on binary bits. ~, &, |, ^ are inverted, bit and and, bit and OR, bit and XOR respectively, as shown in the following table:

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 you specify A = 60; and B = 13; the binary corresponding to the two variables is:

A = 0011 1100

B = 0000 1101

Perform bit operations:

& Bit by bit with. Operate on two numbers by bit and operator, and then return a new number, each bit of which requires that the same bit of both input numbers be 1 to be 1.

Image0

Example: (a & B) the result is 12 and the binary is 0000 1100

\| : bitwise or. Bitwise or operator | compare two numbers and return anew number whose each bit is set to 1 on the condition that neither of the two input digits is 0 (that is, either is 1, or both are 1).

Image1

Example: (a| B) the result is 61 and the binary value is 0011 1101

^ XOR by bit. Compare two numbers by the bitwise XOR operator ^, and then return a number whose each bit is set to 1 if the same bit of the two input numbers is different, or 0 if the same.

Image2

Example: (a ^ B) the result is 49 and the binary is 0011 0001

~ Bitwise inverse operator ~ inverts every bit of an Operand.

Image3

Example: (~ A) the result is-61, and the binary is 1100 0011 in 2s complement form.

<< Move to the left bitwise The left shift operator (< <) moves all bitsof the Operand to the left by the specified number of digits.

The following figure shows the result of 11111111 < < 1 (11111111 moved one bit to the left). The blue number indicates the moved bit, the gray indicates the discarded bit, and the vacancy is filled with an orange 0.

Image4

Example: a < < 2 result is 240and binary is 1111 0000

>> Move bitwise to the right The move right operator (> >) moves all bits of the Operand to the right by the specified number of bits.

The following figure shows the results of 11111111 > 1 (11111111 moved one bit to the right). The blue number indicates the moved bit, the gray indicates the discarded bit, and the vacancy is filled with an orange 0.

Image5

Example: a > > 2 result is 15, binary is 0000 1111

The following is a simple example of a bit operation:

import Cocoa

var A = 60  // Binary as 0011 1100
var B = 13 // Binary as 0000 1101

print("A&B the result is:\(A&B)")
print("A|B the result is:\(A|B)")
print("A^B the result is:\(A^B)")
print("~A the result is:\(~A)")

The execution results of the above procedures are as follows:

A&B the result is:12
A|B the result is:61
A^B the result is:49
~A the result is:-61

Assignment operation

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

Operator

Description

Example

=

A simple assignment operation that specifies that the right Operand is assigned to the left Operand.

C = A + B assigns the operation result of A + B to C.

+=

Add and then assign values, add the operands on the left and right sides, and then assign values to the operands on the left.

C + = An is equivalent to C = C + A

-=

Subtract and then assign a value, subtract the left and right operands and then assign a value to the left Operand.

C-= An is equivalent to C = C-A

\*=

Multiply and then assign values, multiply the operands on the left and rightsides and then assign values to the operands on the left.

C*= A equivalent to C = C * A

/=

Divide and assign values, divide the left and right operands and then assignvalues to the left Operand.

C / = A is equivalent to C = C / A

%=

After the remainder, the Operand on the left and the left is assigned to theoperand on the left.

C% = An is equivalent to C = C% A

<<=

Assign values after bitwise left shift

C < < = 2 is equivalent to C = C < < 2

>>=

Move bitwise to the right before assigning

C > > = 2 equals C = C > > 2

&=

Bitwise and post-operation assignment

C & = 2 is equivalent to C = C & 2

^=

Assign a value after a bitwise XOR operator

C ^ = 2 equals C = C ^ 2

\|=

Assign values after bitwise or operation

C| = 2 equals C = C| 2

The following is a simple example of an assignment operation:

import Cocoa

var A = 10
var B = 20
var C = 100

C = A + B
print("C the result is:\(C)")

C += A
print("C the result is:\(C)")

C -= A
print("C the result is:\(C)")

C *= A
print("C the result is:\(C)")

C /= A
print("C the result is:\(C)")

//The following tests have been annotated, and each
instance can be tested without annotations
/*
C %= A
print("C the result is:\(C)")


C <<= A
print("C the result is:\(C)")

C >>= A
print("C the result is:\(C)")

C &= A
print("C the result is:\(C)")

C ^= A
print("C the result is:\(C)")

C |= A
print("C the result is:\(C)")
*/

The execution results of the above procedures are as follows:

C the result is:30
C the result is:40
C the result is:30
C the result is:300
C the result is:30

Interval operator

Swift provides operators for two intervals.

Operator

Description

Example

Closed interval operator

The closed interval operator (a.. b) defines an interval that contains all values from a to b (including an and b), b must be greater than or equal to a. ? The closed interval operator is useful when iterating over all values of an interval, such as in a for-in loop:

The interval values of 1. 5 are 1, 2, 3, 4 and 5.

Semi-open interval operator

The semi-open interval (a. < b) defines an interval from a to b but not including b. It is called a semi-open interval because it contains the first value but not the last value.

1..< 5 interval values are 1, 2, 3, and 4

! (a & B) is true.

The following is a simple example of interval operation:

import Cocoa

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

print("Semi open interval operator:")
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
4 * 5 = 20
5 * 5 = 25
Semi 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 unary, binary, and ternary operators.

  • Unary operators operate on a single Operand, such as ( -a ). Unary operators are divided into pre-operators and post-operators, which need to precede the operands (such as !b The post operator needs to be immediately after the Operand such as c! Note:There is no similar c! in Java/C! Syntax, which is used in Swift to take values of Optional types.

  • Binary operators operate on two operands, such as 2 + 3 are centered because they appear between two operands

  • The ternary operator operates on three operands. Like the C language, Swift has only one ternary operator, which is the ternary operator a ? b : c ).

Operator

Description

Example

One yuan minus

Prefix the number with a-sign

-3 or-4

One yuan plus

Prefix the + sign before the number

+6 results in 6

Ternary operator

condition ? X : Y

If condition is true, 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("The value of -A is :\(-A)")
print("The value of A + B is :\(A + B)")
print("Ternary operation:\(C ? A : B )")
print("Ternary operation:\(D ? A : B )")

The execution results of the above procedures are as follows:

The value of -A is:-1
The value of A + B is:3
Ternary operation:1
Ternary operation:2

Operator precedence

An expression may include several data objects with different data types connected by different operators; because the expression has a variety of operations, different operation sequences may result in different results oreven erroneous operations. because when an expression contains a variety ofoperations, it must be combined in a certain order in order to ensure the rationality of the operation and the correctness and uniqueness of the results.

The priority decreases from top to bottom, with the highest priority at the top and the lowest priority for the comma operator.

In the same priority, it is calculated according to the combination order. Most operations are calculated from left to right, and only three priorities are combined from right to left. They are unary operators, conditional operators, and assignment operators.

The basic priorities need to be remembered:

  • The pointer is the best, and monocular operation is better than binocular operation. Such as a plus or minus sign.

  • Multiply and divide (module) first, then add and subtract.

  • First the arithmetic operation, then the shift operation, and finally the bit operation. Please note that 1 < 3 + 2 & 7 is equivalent to (1 < (3 + 2))& 7

  • Final calculation of logical operation

Swift operator precedence (from high to low):

Operator

Example

Bit operator

> > & < < & > >

Multiplication operator

& \* % & * /

Addition operator

| & + &-+-^

Interval operator

… <.

Type conversion operator

Is as

Aggregate Operation of nil

??

Comparison operator

! = > < > = < = =

Logic and operator

& &

Logic or operator

||

Wave arrow

~ >

Ternary operator

?:

Arrowhead function

(.)

Assignment operator

| =% = / = & < < & > > = & =* = > > < < = ^ = + =-=

Here is a simple example of operator precedence:

import Cocoa

var A = 0

A = 2 + 3 * 4 % 5
print("The value of A is:\(A)")

The execution results of the above procedures are as follows:

The value of A is:4

Instance resolution:

According to the operator precedence, the operation of the above program canbe parsed into the following steps, and the expression is equivalent to:

2 + ((3 * 4) % 5)

The first step calculates: (3 * 4) = 12, so the expression is equivalent to:

2 + (12 % 5)

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

2 + 2

At this point, it is easy to see that the calculated result is 4.

Powered by TorCMS (https://github.com/bukun/TorCMS).