Advanced Operators
Bitwise Operators
Bitwise NEGATION: ~ Bitwise AND OPERATION: & Bitwise OR OPERATION: | Bitwise XOR: ^ Bitwise left SHIFT operation: << bitwise Right Move count: >>
overflow operatorSince Swift has provided the so-called type safety. We can no longer use a Int.max +1 operation, which will lead to error. So Swift also provides an overflow operator that allows us to continue with +1 if we know it will overflow, which sounds good.
Overflow addition &+ overflow subtraction &-overflow multiplication &* overflow Division &/overflow Fetch &%
Interestingly, we know a lot of languages. Suppose a number is removed by 0, it generates an error. In Swift, if the overflow division &/is removed by 0, it will not produce an error. Instead, you get a value of 0:
var a = Int.max&/0//No error, then A is assigned a value of 0var B = Int.max/ 0// Error. Due to/no overflow operation
Priority LevelAlthough the document is descriptive of the priority, but I think a normal class is not used so infrequently used rules to write a normal human not so easy to understand the expression, so can ignore the priority introduced here, direct use () to indicate the priority level.
operator OverloadingOperator overloading is still available in Swift:
。。。 It's ridiculous not to offer.
However, the definition of the central, front, and post operators is actually different.
。
。
Mid-mount: @infix
The middle operator also contains = =,! =, and so on
Front: @prefix@prefix func+ (num:Int),Int{
returnnum +5
}
varA =3
println (+a) //result is 8
Rear: @postfixAlmost the same as the predecessor, it is not written.
Combine assignment operators: @assignment
define the operator yourself
operatorPrefix + + + + {}//defines a predecessor operator + + +,Note the keyword of the definition operator operator
@prefix @assignment func +++ (inoutNum:Int) {
num + =Ten
}
varA =1
+++a
println (a)//Get Results
you define the binding and precedence of the central operator
operator represents the definition of a new operator, infix is the middle operator, the operator writes +-, and the associativity represents the binding, which is represented as a left-associative with the back. Precedence represents the priority level. Together with the following numbers, the priority is 140 (The default value is)
Written at the end:don't play with the operator!!
!!
Swift notes (21)--Advanced operators