Swift notes (21)--Advanced operators

Source: Internet
Author: User
Tags bitwise bitwise operators

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.