Swift Int Examples: Int.max, Int.min

Use the Int type and the Int.max and Int.min properties. Test Ints with MemoryLayout.

Int. Numbers are used throughout Swift programs. With an Int, we have an integral type that can be used in expressions, collections and loops.

Int is important. In Swift we find it is 8 bytes and can accommodate large numbers. It does not support fractional values. We can handle overflow with special methods.

First example. Let us run through some Int properties. First we use MemoryLayout on the Int type. This returns the byte count in an Int which is 8.

Max, min: These are useful properties on the Int. As we see in the output, an Int in Swift is like a long in other languages.

Advanced: This adds to the Int. We can just use an add operator like "+=" for most programs.

Tip: To go to the previous number, we can use advanced() by negative one. But subtracting one is likely clearer.

Swift program that shows Int usage // Display byte size of Int. let size = MemoryLayout<Int>.size print(size) // Display maximum Integer value. let max = Int.max print(max) // Display minimum Integer value. let min = Int.min print(min) // Use advanced to increment an Int. var value = 100 let result = value.advanced(by: 10) print(result) // Use advanced to move forward and back. let next = result.advanced(by: 1) print(next) let previous = result.advanced(by: -1) print(previous) Output 8 9223372036854775807 -9223372036854775808 110 111 109

MultiplyWithOverflow. In an overflow, a number cannot be represented using the available bits. This causes a logic problem and unexpected results.Tuple

Part 1: We try to capture overflows with multiplyWithOverflow. We perform a multiply that causes an overflow.

Part 2: Here the numbers multiplied do not overflow, so the "overflow" property returns false.

Tip: Other methods include addWithOverflow, divideWithOverflow, remainderWithOverflow and subtractWithOverflow.

Swift program that uses multiplyWithOverflow // Part 1: do a multiply that overflows. let res1 = Int.multiplyWithOverflow(Int.max, 2) print(res1) // Test whether it overflowed. if res1.overflow { print("An overflow happened") } // Part 2: do a multiply that will not overflow. let res2 = Int.multiplyWithOverflow(2, 2) print(res2) // We did not have an overflow, so print the 0 value. if !res2.overflow { print(res2.0) } Output (-2, true) An overflow happened (4, false) 4

Overflow operators. By default in Swift an overflow causes an error. With the overflow add, subtract and multiply operators, this error does not appear.

Instead: The numeric value will become invalid. It will wrap around to the lowest (or highest) value.

Swift program that uses overflow add operator // This number cannot be incremented without overflow. var number = Int8.max print(number) // Use overflow operator to avoid an error. // ... Add 1 to number. number = number &+ 1 print(number) Output 127 -128 Operators: &+ Overflow addition &- Overflow subtraction &* Overflow multiplication

Overflow research. In the Swift documentation we find a detailed description (with pictures) about how overflow works. The low-level details are standard.

Quote: For both signed and unsigned integers, overflow in the positive direction wraps around from the maximum valid integer value back to the minimum, and overflow in the negative direction wraps around from the minimum value to the maximum.

Advanced Operators:

Convert to String. With the String init method, we can convert an Int into a String. This always succeeds. Every possible Int can be represented as a String.Convert

Numbers are everywhere. Int is used in nearly every program. It is 8 bytes, which makes it similar to "long" values in other languages.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to