BinaryInteger
声明
protocol BinaryInteger : CustomStringConvertible, Hashable, Numeric, Strideable where Self.Magnitude : BinaryInteger, Self.Magnitude == Self.Magnitude.Magnitude
概述
BinaryInteger
协议是标准库提供的所有整数类型的基础。所有标准库的整数类型,例如 Int
和 UInt32
,都符合 BinaryInteger
。
数值类型之间的转换
您可以从浮点数或任何类型的另一个二进制整数创建符合 BinaryInteger
协议的类型的新实例。 BinaryInteger
协议为四种不同类型的转换提供了初始化程序。
Range-Checked 转换
当您确定传递的值可以在新类型中表示时,您可以使用默认的 init(_:)
初始化程序来创建新实例。例如,Int16
的实例可以表示值 500
,因此下面代码示例中的第一次转换成功。相同的值太大而无法表示为 Int8
实例,因此第二次转换失败,触发运行时错误。
let x: Int = 500
let y = Int16(x)
// y == 500
let z = Int8(x)
// Error: Not enough bits to represent...
当您使用默认初始值设定项从浮点值创建二进制整数时,在检查范围之前,该值将向零舍入。在以下示例中,值 127.75
被四舍五入为 127
,可由 Int8
类型表示。 128.25
舍入为 128
,它不能表示为 Int8
实例,从而触发运行时错误。
let e = Int8(127.75)
// e == 127
let f = Int8(128.25)
// Error: Double value cannot be converted...
精确转换
在检查传递的值是否可表示后,使用 init?(exactly:)
初始化程序创建一个新实例。而不是捕获超出范围的值,而是使用可失败的 init?(exactly:)
初始化程序导致 nil
。
let x = Int16(exactly: 500)
// x == Optional(500)
let y = Int8(exactly: 500)
// y == nil
转换浮点值时,init?(exactly:)
初始化程序会检查传递的值是否没有小数部分以及该值是否可以在结果类型中表示。
let e = Int8(exactly: 23.0) // integral value, representable
// e == Optional(23)
let f = Int8(exactly: 23.75) // fractional value, representable
// f == nil
let g = Int8(exactly: 500.0) // integral value, nonrepresentable
// g == nil
夹紧转换
使用 init(clamping:)
初始化程序创建二进制整数类型的新实例,其中超出范围的值被限制在该类型的可表示范围内。对于类型 T
,结果值在 T.min...T.max
范围内。
let x = Int16(clamping: 500)
// x == 500
let y = Int8(clamping: 500)
// y == 127
let z = UInt8(clamping: -500)
// z == 0
位模式转换
使用 init(truncatingIfNeeded:)
初始化程序创建一个与传递的值具有相同位模式的新实例,并根据需要扩展或截断值的表示。请注意,可能不会保留该值,特别是在有符号整数类型与无符号整数类型之间转换或目标类型的位宽小于源类型时。以下示例显示了非负整数的扩展和截断如何工作:
let q: Int16 = 850
// q == 0b00000011_01010010
let r = Int8(truncatingIfNeeded: q) // truncate 'q' to fit in 8 bits
// r == 82
// == 0b01010010
let s = Int16(truncatingIfNeeded: r) // extend 'r' to fill 16 bits
// s == 82
// == 0b00000000_01010010
任何填充都由 sign-extending
传递的值执行。当扩展非负整数时,结果用零填充。当扩展负整数时,结果用 1 填充。此示例显示了负值的几个扩展转换 - 请注意,即使转换为无符号类型,负值也是 sign-extended。
let t: Int8 = -100
// t == -100
// t's binary representation == 0b10011100
let u = UInt8(truncatingIfNeeded: t)
// u == 156
// u's binary representation == 0b10011100
let v = Int16(truncatingIfNeeded: t)
// v == -100
// v's binary representation == 0b11111111_10011100
let w = UInt16(truncatingIfNeeded: t)
// w == 65436
// w's binary representation == 0b11111111_10011100
比较整数类型
您可以使用关系运算符,例如小于和等于运算符(<
和 ==
)来比较不同二进制整数类型的实例。以下示例比较了 Int
、 UInt
和 UInt8
类型的实例:
let x: Int = -23
let y: UInt = 1_000
let z: UInt8 = 23
if x < y {
print("\(x) is less than \(y).")
}
// Prints "-23 is less than 1000."
if z > x {
print("\(z) is greater than \(x).")
}
// Prints "23 is greater than -23."
可用版本
相关用法
- Swift BinaryInteger quotientAndRemainder(dividingBy:)用法及代码示例
- Swift BinaryInteger trailingZeroBitCount用法及代码示例
- Swift BinaryInteger init(clamping:)用法及代码示例
- Swift BinaryInteger <<=(_:_:)用法及代码示例
- Swift BinaryInteger !=(_:_:)用法及代码示例
- Swift BinaryInteger /(_:_:)用法及代码示例
- Swift BinaryInteger init(truncatingIfNeeded:)用法及代码示例
- Swift BinaryInteger ==(_:_:)用法及代码示例
- Swift BinaryInteger init(_:)用法及代码示例
- Swift BinaryInteger %=(_:_:)用法及代码示例
- Swift BinaryInteger &=(_:_:)用法及代码示例
- Swift BinaryInteger |(_:_:)用法及代码示例
- Swift BinaryInteger +(_:_:)用法及代码示例
- Swift BinaryInteger &(_:_:)用法及代码示例
- Swift BinaryInteger ^=(_:_:)用法及代码示例
- Swift BinaryInteger ^(_:_:)用法及代码示例
- Swift BinaryInteger %(_:_:)用法及代码示例
- Swift BinaryInteger *(_:_:)用法及代码示例
- Swift BinaryInteger -(_:_:)用法及代码示例
- Swift BinaryInteger /=(_:_:)用法及代码示例
- Swift BinaryInteger >>=(_:_:)用法及代码示例
- Swift BinaryInteger ~(_:)用法及代码示例
- Swift BinaryInteger |=(_:_:)用法及代码示例
- Swift BinaryInteger <<(_:_:)用法及代码示例
- Swift BinaryInteger >>(_:_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 BinaryInteger。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。