Swift overflow operator

Source: Internet
Author: User

By default, when you assign an integer constant or variable to a large number that it cannot host, Swift will not let you do so, and it will complain. In this way, the operation is too large or too small number of time is very safe.

For example, the integer range that the Int16 integral can host is-32768 to 32767, and if it is assigned more than this range, an error is made:

var potentialoverflow = Int16.max
//Potentialoverflow equals 32767, which is the largest integer Int16 can host
Potentialoverflow = 1
//OH , there was a mistake.

Error handling of large or too small numbers makes your numerical boundary conditions more flexible.

Of course, you intentionally truncate a valid bit when it overflows, and you can use overflow rather than error handling. Swfit provides an overflow operator with 5 & symbols at the beginning of an integer calculation. Overflow addition &+ overflow subtraction &-overflow multiplication &* Overflow Division &/overflow residual &% value overflow

The following example uses the overflow addition &+ to dissect the overflow of an unsigned integer

var willoverflow = Uint8.max
//Willoverflow equals UInt8 maximum integer 255 willoverflow
= willoverflow &+ 1
//This time would Overflow equals 0.

Willoverflow the maximum value of 255 (binary 11111111) that can be carried by Int8, and then use &+ plus 1. Then UInt8 can not express the new value of the binary, it caused the new value overflow, we could look at the picture below. After the overflow, the new value in the UInt8 load range is 00000000, that is, 0.

value of the bottom overflow

Numeric values can also be crossed because they are too small. As an example:

The minimum value for the UInt8 is 0 (binary is 00000000). Using &-to reduce the overflow by 1, you will get the binary 11111111 decimal 255.

The SWIFT code is this:

var willunderflow = uint8.min
//willunderflow equals UInt8 minimum 0
willunderflow = willunderflow &-1//
at this time would Underflow equals 255.

There is a similar overflow in the character integral, and all subtraction of the character integer is also binary subtraction for binary numbers including the sign bit, as mentioned in the section "bitwise LEFT/Right shift operator". The smallest signed integer is-128, or 10000000 of the binary. By subtracting 1 from the overflow subtraction, it becomes 01111111, the maximum integer 127 that UInt8 can host.

Take a look at the SWIFT code:

var signedunderflow = int8.min
//Signedunderflow equals smallest signed integer -128
signedunderflow = signedunderflow &-1
Now Signedunderflow equals 127.
except 0 overflow

A number can result in an error except for 0 i/0, or 0 for the remainder I 0.

Let x = 1 let
y = x/0

The operators &/and &% with their corresponding overflow versions are given a value of 0, except for the 0 operation.

Let x = 1 let
y = x &/0
//y equals 0
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.