Advanced operators in Swift provide the programmer with increased functionality and complex value manipulation. They build upon the concepts covered in the basic operators wiki and include compound assignments, bitwise, and custom operators, to name a few.
Bitwise operators are used to manupilate the raw data bits of a structure, such as graphics, file inputs, or device drivers, while not common, these operators are very useful in their specific use cases. Swift also allows the overloading of the standard operators to include your own definition of the function.
Compound Assignment Operator
Compound assignment operators combine the assignment operator
= with another separate operation, for example
+. The following operations are allowed
Bitwise NOT Operator
~ operator inverts all the bits in a number, so
0101 0101 will become
1010 1010. It is very important to note that using the
~ operator on a signed integer, a standard integer, inverts a positive integer, n, into -(n+1) and not the inverse binary.
Bitwise AND Operator
& operator returns a new binary from the combination of two other binary numbers. If there is a
1 in the same place in each binary string, the result will have a
1 in that digit, else, a
0 will be place
Bitwise OR Operator
| operator compares two numbers and returns a new number where each bit is set to
1 if either bit equals
1 in the original two numbers.
Bitwise XOR Operator
^ operator, also known as ‘exclusive OR’, compares two binary numbers. It will return a new number with each bit set to
1 if the bits of the orignal numbers are different, or exclusive.
0‘s will be placed anywhere that the two bits are equivalent.
Bitwise Left and Right Shift Operators
Bitwise shift operators,
>> operate slightly differently on signed and unsigned values. For unsigned values the process is very straight forward, the number shifts to the left or right by the number of digits specified. In essence, for every shift to the left you make, you are doubling the value of the number and for every shift right, you are halving the number.
In signed numbers, the first digit represents the sign,
0 for positive,
1 for negative. For this reason, it is important to keep that digit as we shift either direction. Let’s start with an 8-bit signed number, 15. We said the first digit was the sign, 15 is positive so the sign is
0 and the remaining 7 bits are:
0001111. Together our 8-bit number is
00001111. When shifted left, the signed digit remains the same and digits are “pushed” out of the binary number at the signed digit. When shifting right, instead of filling the void space with a
0, it is filled with the signed digit.
Custom and Overloaded Operators
Swift allows you to create your own operators or even define ones that already exist for a given class or structure. Let’s create a basic student structure that we’ll use for the examples.
The equatable operator,
== and it’s inverse
!= will use Swift’s standard definition unless it is redefined. We can test the equatability of the two students by adding conformance to
Equatable and creating a
We can now use
== to compare two students in a way that makes sense to our program logic. Swift also allows us to overright most of the common operators, for a complete list, visit this page. When we add or subtract from a student, we want to manipulate the GPAs, the mthods below provide examples of their implementation.
We can redefine the
- operators to adjust the GPAs of our students, we also add additional logic to the operator so that it doesn’t go above
4.0 or below
Basic operators are not the only ones that can be reassigned, we can also redefine compound operators as well.
The above examples are not inclusive of all the tasks that can be reassigned, and custom operators such as
-+- can also be created for example. Use the above reference to Swift documentation for the complete list.