YouTip LogoYouTip

Swift Operators

An operator is a symbol that tells the compiler to perform a mathematical or logical operation.\n\nSwift provides the following types of operators:\n\n* Arithmetic Operators\n* Comparison Operators\n* Logical Operators\n* Bitwise Operators\n* Assignment Operators\n* Range Operators\n* Other Operators\n\nIn this chapter, we will introduce arithmetic operators, comparison operators, logical operators, bitwise operators, assignment operators, and other operators in detail.\n\n* * *\n\n## Arithmetic Operators\n\nThe following table lists the arithmetic operators supported by Swift, where variable A is 10 and variable B is 20:\n\n| Operator | Description | Example |\n| --- | --- | --- |\n| + | Plus | A + B results in 30 |\n| βˆ’ | Minus | A βˆ’ B results in -10 |\n| * | Multiply | A * B results in 200 |\n| / | Divide | B / A results in 2 |\n| % | Modulus | B % A results in 0 |\n\n> **Note:** ++ and -- have been deprecated in Swift 3.\n\n### Examples\n\nThe following are simple examples of arithmetic operations:\n\n
import Cocoavar A = 10var B = 20print("A + B Result:(A + B)")print("A - B Result:(A - B)")print("A * B Result:(A * B)")print("B / A Result:(B / A)") A += 1 // Similar to A++print("A += 1 After A, the value is (A)") B -= 1 // Similar to B--print("B -= 1 After B, the value is (B)")
\n\nThe execution results of the above program are:\n\n
A + B Result:30 A - B Result:-10 A * B Result:200 B / A Result:2 A += 1 After A, the value is 11 B -= 1 After B, the value is 19
\n\n* * *\n\n## Comparison Operators\n\nThe following table lists the comparison operators supported by Swift, where variable A is 10 and variable B is 20:\n\n| Operator | Description | Example |\n| --- | --- | --- |\n| == | Equal to | (A == B) is false. |\n| != | Not equal to | (A != B) is true. |\n| > | Greater than | (A > B) is false. |\n| < | Less than | (A = | Greater than or equal to | (A >= B) is false. |\n| <= | Less than or equal to | (A <= B) is true. |\n\n### Examples\n\nThe following are simple examples of comparison operations:\n\n
import Cocoavar A = 10var B = 20print("A == B Result:(A == B)")print("A != B Result:(A != B)")print("A > B Result:(A > B)")print("A < B Result:(A = B Result:(A >= B)")print("A <= B Result:(A <= B)")
\n\nThe execution results of the above program are:\n\n
A == B Result: false A != B Result: true A > B Result: false A = B Result: false A <= B Result: true
\n\n* * *\n\n## Logical Operators\n\nThe following table lists the logical operators supported by Swift, where variable A is true and variable B is false:\n\n| Operator | Description | Example |\n| --- | --- | --- |\n| && | Logical AND. If both sides of the operator are TRUE, then it is TRUE. | (A && B) is false. |\n| || | Logical OR. If at least one side of the operator is TRUE, then it is TRUE. | (A || B) is true. |\n| ! | Logical NOT. Inverts the boolean value, making true become false, and false become true. | !(A && B) is true. |\n\nThe following are simple examples of logical operations:\n\n
import Cocoavar A = truevar B = falseprint("A && B Result:(A && B)")print("A || B Result:(A || B)")print("!A Result:(!A)")print("!B Result:(!B)")
\n\nThe execution results of the above program are:\n\n
A && B Result: false A || B Result: true!A Result: false!B Result: true
\n\n* * *\n\n## Bitwise Operators\n\nBitwise operators are used to operate on binary bits. ~, &, |, ^ represent NOT, bitwise AND, bitwise OR, and bitwise XOR respectively, as shown in the table below:\n\n| p | q | p & q | p | q | p ^ q |\n| --- | --- | --- | --- | --- |\n| 0 | 0 | 0 | 0 | 0 |\n| 0 | 1 | 0 | 1 | 1 |\n| 1 | 1 | 1 | 1 | 0 |\n| 1 | 0 | 0 | 1 | 1 |\n\nIf specified A = 60; and B = 13; the binary representations of the two variables are:\n\n
A = 0011 1100 B = 0000 1101
\n\nPerforming bitwise operations:\n\n| Operator | Description | Diagram | Example |\n| --- | --- | --- | --- |\n| & | Bitwise AND. The bitwise AND operator operates on two numbers and returns a new number. Each bit of this new number is 1 only if the corresponding bits of both input numbers are 1. | !(#) | (A & B) results in 12, binary is 0000 1100 |\n| | | Bitwise OR. The bitwise OR operator | compares two numbers and returns a new number. The condition for each bit of this number to be set to 1 is that the corresponding bits of the two input numbers are not both 0 (i.e., either one is 1, or both are 1). | !(#) | (A | B) results in 61, binary is 0011 1101 |\n| ^ | Bitwise XOR. The bitwise XOR operator ^ compares two numbers and returns a number. Each bit of this number is set to 1 if the corresponding bits of the two input numbers are different, and set to 0 if they are the same. | !(#) | (A ^ B) results in 49, binary is 0011 0001 |\n| ~ | Bitwise NOT. The bitwise NOT operator ~ inverts every bit of the operand. | !(#) | (~A ) results in -61, binary is 1100 0011 in 2's complement form. |\n| << | Bitwise Left Shift. The left shift operator (<<) moves all bits of the operand to the left by the specified number of bits. | The following figure shows the result of 11111111 << 1 (11111111 left shifted by 1 bit). Blue numbers indicate shifted bits, gray indicates discarded bits, and empty bits are filled with orange 0s. !(#) | A <> | Bitwise Right Shift. 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 right shifted by 1 bit). Blue numbers indicate shifted bits, gray indicates discarded bits, and empty bits are filled with orange 0s. !(#) | A >> 2 results in 15, binary is 0000 1111 |\n\nThe following are simple examples of bitwise operations:\n\n
import Cocoavar A = 60 // Binary is 0011 1100 var B = 13 // Binary is 0000 1101 print("A&B Result:(A&B)")print("A|B Result:(A|B)")print("A^B Result:(A^B)")print("~A Result:(~A)")
\n\nThe execution results of the above program are:\n\n
A&B Result: 12 A|B Result:61 A^B Result:49~A Result:-61
\n\n* * *\n\n## Assignment Operators\n\nThe following table lists the basic assignment operators in Swift:\n\n| Operator | Description | Example |\n| --- | --- | --- |\n| = | Simple assignment, assigns the right operand to the left operand. | C = A + B assigns the result of A + B to C |\n| += | Add and assign, adds the left and right operands and then assigns the result to the left operand. | C += A is equivalent to C = C + A |\n| -= | Subtract and assign,
← Swift Decision MakingSwift Literals β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.