实例方法
multiplied
multipliedFullWidth(by:)
返回一个元组,其中包含将此值乘以给定值的结果的高位和低位部分。
声明
func multipliedFullWidth(by other: UInt) -> (high: UInt, low: UInt.Magnitude)
返回值
一个元组,包含将此值与 other
相乘的结果的高位和低位部分。
参数
other
将此值乘以的值。
详述
使用此方法计算否则会溢出的产品的完整结果。与传统的截断乘法不同,multipliedFullWidth(by:)
方法返回一个元组,其中包含该值与 other
乘积的 high
和 low
部分。以下示例使用此方法将两个在相乘时通常会溢出的 UInt8
值相乘:
let x: UInt8 = 100
let y: UInt8 = 20
let result = x.multipliedFullWidth(by: y)
// result.high == 0b00000111
// result.low == 0b11010000
x
和 y
的乘积是 2000,它太大而无法在 UInt8
实例中表示。 result
值的high
和low
属性在连接形成double-width 整数时表示2000;也就是说,使用result.high
作为高字节,result.low
作为UInt16
实例的低字节。
let z = UInt16(result.high) << 8 | UInt16(result.low)
// z == 2000
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift UInt +(_:)用法及代码示例
- Swift UInt &<<(_:_:)用法及代码示例
- Swift UInt leadingZeroBitCount用法及代码示例
- Swift UInt init(_:radix:)用法及代码示例
- Swift UInt quotientAndRemainder(dividingBy:)用法及代码示例
- Swift UInt &(_:_:)用法及代码示例
- Swift UInt &*(_:_:)用法及代码示例
- Swift UInt init(truncatingIfNeeded:)用法及代码示例
- Swift UInt *=(_:_:)用法及代码示例
- Swift UInt %(_:_:)用法及代码示例
- Swift UInt &-(_:_:)用法及代码示例
- Swift UInt +=(_:_:)用法及代码示例
- Swift UInt ==(_:_:)用法及代码示例
- Swift UInt |=(_:_:)用法及代码示例
- Swift UInt >>=(_:_:)用法及代码示例
- Swift UInt !=(_:_:)用法及代码示例
- Swift UInt +(_:_:)用法及代码示例
- Swift UInt nonzeroBitCount用法及代码示例
- Swift UInt <<=(_:_:)用法及代码示例
- Swift UInt ..<(_:)用法及代码示例
- Swift UInt ...(_:_:)用法及代码示例
- Swift UInt init(exactly:)用法及代码示例
- Swift UInt &+=(_:_:)用法及代码示例
- Swift UInt ^(_:_:)用法及代码示例
- Swift UInt &*=(_:_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 UInt multipliedFullWidth(by:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。