Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Swift operator


May 21, 2021 Swift


Table of contents


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
  • The logical operator
  • Bit operator
  • The assignment operator
  • The interval operator
  • Other operators

In this section, we'll cover arithmetic operators, relationship operators, logical operators, bit operators, assignment operators, and other operators in more detail.


Arithmetic operator

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

Operator Describe Instance
+ Plus The result is 30
Minus sign The A-B result is -10
* Multiplier The result is 200
/ Divide the sign The B/A result is 2
% Ask for more B % A results in 0
++ Self-increase The result is 11
-- Self-reducing A-- The result is 9

Instance

Here are a simple examples of arithmetic:

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 above procedures are performed as follows:

A + B 结果为:30
A - B 结果为:-10
A * B 结果为:200
B / A 结果为:2
A++ 后 A 的值为 11
B-- 后 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:

Operator Describe Instance
== Equals (A s b) is false.
!= Not equal to (A!-B) is true.
> Greater than (A and B) is false.
< Less than (A slt; B) is true.
>= Is greater than or equal to (A and B) is false.
<= Less than or equal to (A -lt;?B) is true.

Instance

Here are a simple examples of comparison operations:

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 above procedures are performed as follows:

A == B 结果为:false
A != B 结果为:true
A > B 结果为:false
A < B 结果为:true
A >= B 结果为:false
A <= B 结果为:true

The logical operator

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

Operator Describe Instance
&& Logic and. TRUE if both sides of the operator are TRUE. (A and B) is false.
|| Logic or. TRUE if at least one of the operators is TRUE on either side. (A || B) true.
! Logic is not. Boolean values are reversed, making true false, false become true. ! (A and B) is true.

Here are a simple examples of logical operations:

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 above procedures are performed as follows:

A && B 结果为:false
A || B 结果为:true
!A 结果为:false
!B 结果为:true

Bit operator

Bit operators are used to operate on binary bits, | ,

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; a nd B s 13; The binary for the two variables is:

A = 0011 1100

B = 0000 1101

To perform bit operations:

Operator Describe Graphic Instance
& By bit with. Operates on two numbers by bit and operator, and then returns a new number, each of which requires the same bit of two input numbers to be 1. Swift operator The result is 12 and the binary is 0000 1100
| By bit or. Compare two numbers by bit or operator| and return a new number, each of which sets 1 if neither input number has the same bit 0 (i.e. either one is 1, or both are 1). Swift operator (A | B) The result is 61 and the binary is 0011 1101
^ By bit or. Compare two numbers by bit difference or operator, and then return a number, each bit of which is set to 1 if the same bit of the two input numbers is different, and if the same is set to 0. Swift operator The result is 49 and the binary is 0011 0001
~ The bit-by-bit counteroperation operator is reversed for each bit of an operatic number. Swift operator The result is -61, with binary of 1100 0011 in 2's couple form.