实例方法
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:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。