實例方法
multiplied
multipliedFullWidth(by:)
返回一個元組,其中包含將此值乘以給定值的結果的高位和低位部分。
必需的。提供默認實現。
聲明
func multipliedFullWidth(by other: Self) -> (high: Self, low: Self.Magnitude)
返回值
一個元組,包含將此值與 other
相乘的結果的高位和低位部分。
參數
other
將此值乘以的值。
詳述
使用此方法計算否則會溢出的產品的完整結果。與傳統的截斷乘法不同,multipliedFullWidth(by:)
方法返回一個元組,其中包含該值與 other
乘積的 high
和 low
部分。以下示例使用此方法將兩個在相乘時通常會溢出的 Int8
值相乘:
let x: Int8 = 48
let y: Int8 = -40
let result = x.multipliedFullWidth(by: y)
// result.high == -8
// result.low == 128
x
和 y
的乘積是 -1920
,它太大而無法在 Int8
實例中表示。 result
值的 high
和 low
組件在連接形成 double-width 整數時表示 -1920
;也就是說,使用result.high
作為高字節,result.low
作為Int16
實例的低字節。
let z = Int16(result.high) << 8 | Int16(result.low)
// z == -1920
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift FixedWidthInteger &-=(_:_:)用法及代碼示例
- Swift FixedWidthInteger &<<=(_:_:)用法及代碼示例
- Swift FixedWidthInteger &<<(_:_:)用法及代碼示例
- Swift FixedWidthInteger &-(_:_:)用法及代碼示例
- Swift FixedWidthInteger &*=(_:_:)用法及代碼示例
- Swift FixedWidthInteger nonzeroBitCount用法及代碼示例
- Swift FixedWidthInteger init(_:radix:)用法及代碼示例
- Swift FixedWidthInteger random(in:)用法及代碼示例
- Swift FixedWidthInteger &>>=(_:_:)用法及代碼示例
- Swift FixedWidthInteger dividingFullWidth(_:)用法及代碼示例
- Swift FixedWidthInteger leadingZeroBitCount用法及代碼示例
- Swift FixedWidthInteger &>>(_:_:)用法及代碼示例
- Swift FixedWidthInteger random(in:using:)用法及代碼示例
- Swift FixedWidthInteger &+=(_:_:)用法及代碼示例
- Swift FixedWidthInteger &*(_:_:)用法及代碼示例
- Swift FixedWidthInteger &+(_:_:)用法及代碼示例
- Swift FixedWidthInteger用法及代碼示例
- Swift FlattenSequence prefix(_:)用法及代碼示例
- Swift Float -(_:)用法及代碼示例
- Swift FlattenSequence max(by:)用法及代碼示例
- Swift Float80 /(_:_:)用法及代碼示例
- Swift Float ...(_:_:)用法及代碼示例
- Swift FloatingPointRoundingRule.toNearestOrAwayFromZero用法及代碼示例
- Swift Float16 ...(_:_:)用法及代碼示例
- Swift FloatingPointRoundingRule.up用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 FixedWidthInteger multipliedFullWidth(by:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。