# Swift Learning Notes _ Data type _ integer type

Source: Internet
Author: User

1 integer type

An integer is a number that has no fractional part.

There are two types of integers:

(1) signed integer type: denotes negative integer, 0, and positive integer

(2) unsigned integer type: represents 0, and positive integers

There are 5 ways to represent signed integer types:

(1) Int8:8 bits in memory, the range indicated is: -128~127

(2) Int16:16 bits in memory, the range indicated is: -32768~32767

(3) Int32:32 bits in memory, the range indicated is: -2147483648~2147483647

(4) Int64:64 bits in memory, the range indicated is: -9223372036854775808~9223372036854775807

(5) Int: In a 32-bit system, int = Int32; In a 64-bit system, int = Int64.

In addition to memory optimization and performance tuning, it is recommended that you always use int to represent a signed integer type.

Can improve the consistency and reusability of code.

※ The first digit is the sign bit

`Let int8min:int8 = int8.min//-128let int8max:int8 = Int8.max//127let int16min:int16 = int16.min//-32768let int16max:Int Int16.max//32767let int32min:int32 = int32.min//-2147483648let int32max:int32 = Int32.max//2147483647let int64min: Int64 = int64.min//-9223372036854775808let int64max:int64 = Int64.max//9223372036854775807let intmin:Int = Int.min//-9 223372036854775808let intmax:int = Int.max//9223372036854775807`

Unsigned integer types are represented in 5 ways:

(1) UInt8:8 bits in memory, the range indicated is: 0~255

(2) UInt16:16 bits in memory, the range indicated is: 0~65535

(3) UInt32:32 bits in memory, the range indicated is: 0~4294967295

(4) UInt64:64 bits in memory, the range indicated is: 0~18446744073709551615

(5) UINT: In a 32-bit system, UINT = UInt32; In a 64-bit system, UINT = UInt64.

It is recommended to use an int instead of a uint, even if you know that the stored values are non-negative.

To improve the consistency and reusability of your code.

※ No sign bit

`Let uint8min:uint8 = uint8.min//0let uint8max:uint8 = Uint8.max//255let uint16min:uint16 = UInt16.min//0let Uint16max:U Int16 = Uint16.max//65535let uint32min:uint32 = uint32.min//0let uint32max:uint32 = Uint32.max//4294967295let uint64min : UInt64 = uint64.min//0let uint64max:uint64 = Uint64.max//18446744073709551615let uintmin:uint = UInt.min//0let Uintmax : UInt = Uint.max//18446744073709551615`

You can access the Min and Max properties of different integer types to get the minimum and maximum values for the corresponding type.

If a variable or constant is declared and initialized without specifying a data type, the system is inferred by default as an int type.

`var tmpint8:int8 =//error Integer literal overflows when stored into int8//var tmpint8:int8 = -129//error Inte GER literal-129 overflows when stored into int8var tmpint8:int8 = 127//tmpint8 = tmpInt8 + 1//errorvar tmpint =//int`

There are 4 ways to represent the literal value of an integer type:

(1) Decimal: Default binary

* Uppercase letters are not allowed: B, O, X

`var tmpbinary = 0b11011//27//var tmpbinary2 = 0b1101//errorvar tmpocx = 0o23//19//var tmpocx2 = 0o23//var Tmphex = 0x4 59F//17823//var tmphex2 = 0x459f//error`

You can add several 0 or _ to the literal value of an integer type to improve readability.

`Let Int64maxconstant:int64 = 0922_3372_0368_5477_5807let Uint64maxconstant:uint64 = 18446_74407_37095_51615`

If you are doing a variable or constant of two different integer types, you must make an explicit type conversion.

`var tmpint8:int8 = 12var tmpint16:int16 = 30//tmpint8 = tmpint16//error Cannot assign value of type Int16 to type int8tmp int8 = Int8 (tmpint16)//30//tmpint16 = Tmpint8tmpint8 = 40tmpint16 = Int16 (tmpint8)//40//tmpint8 + tmpint16//errortmpint 8 + Int8 (tmpint16)//80int16 (tmpint8) + tmpint16//80var result = Tmpint8 + Int8 (tmpint16)//int8//var result:int16 = Tmpi Nt8 + Int8 (tmpint16)//error`

Swift Learning Notes _ Data type _ integer type

Related Keywords:
Related Article

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.