init<T>(truncatingIfNeeded : T)
An integer to convert to this type. この型へと変換する整数。
Availability
Technology
init<T>(truncatingIfNeeded source: T) where T : BinaryInteger
source
An integer to convert to this type. この型へと変換する整数。
When the bit width of T
(the type of source
) is equal to or greater than this type’s bit width, the result is the truncated least-significant bits of source
. For example, when converting a 16-bit value to an 8-bit type, only the lower 8 bits of source
are used.
T
(source
の型)のビット幅がこの型のもつビット幅と等しいかより大きいならば、結果はsource
の先端を切った最下位ビットです。例えば、16bit値を8bit型へ変換する場合、source
の最も下位の8ビットが使われます。
let p: Int16 = -500
// 'p' has a binary representation of 11111110_00001100
let q = Int8(truncatingIfNeeded: p)
// q == 12
// 'q' has a binary representation of 00001100
When the bit width of T
is less than this type’s bit width, the result is sign-extended to fill the remaining bits. That is, if source
is negative, the result is padded with ones; otherwise, the result is padded with zeros.
T
のビット幅がこの型のもつビット幅より小さいならば、結果は残りのビットを満たすように符号拡張されます。すなわち、source
が負ならば、結果は1で詰め物をされます;そうでなければ、結果はゼロで詰め物をされます。
let u: Int8 = 21
// 'u' has a binary representation of 00010101
let v = Int16(truncatingIfNeeded: u)
// v == 21
// 'v' has a binary representation of 00000000_00010101
let w: Int8 = -21
// 'w' has a binary representation of 11101011
let x = Int16(truncatingIfNeeded: w)
// x == -21
// 'x' has a binary representation of 11111111_11101011
let y = UInt16(truncatingIfNeeded: w)
// y == 65515
// 'y' has a binary representation of 11111111_11101011
init<T>(truncatingIfNeeded : T)
init<T>(T)
init<T>(clamping: T)