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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。