Converting Integer Types with Bit Patterns in Swift

To maintain bit patterns while converting integer types in Swift, utilize the provided initializer.

init<T>(truncatingIfNeeded source: T) where T : BinaryInteger

You can employ this initializer method to convert between unsigned and signed integers. For example, 0x00, 0x7F, 0x80, and 0xFF in UInt8 would look like this in decimal.

Hex Decimal in UInt8
0x00 0
0x7F 127
0x80 128
0xFF 255

The same value for Int8 would look like this.

HexDecimal in Int8
0x00 0
0x7F 127
0x80 -128
0xFF -1

You can confirm that you get the expected conversion by typing the following in Playground and running it.

let u1 = UInt8(0x00)
let u2 = UInt8(0x7F)
let u3 = UInt8(0x80)
let u4 = UInt8(0xFF)

let i1 = Int8(truncatingIfNeeded: u1)
let i2 = Int8(truncatingIfNeeded: u2)
let i3 = Int8(truncatingIfNeeded: u3)
let i4 = Int8(truncatingIfNeeded: u4)
Check with Playground
Check with Playground

The system uses the next initializer if you don’t specify an argument label.

init<T>(_ source: T) where T : BinaryInteger

While this initializer can also convert, it triggers an error when the value falls outside the acceptable range. Specifically, the conversion from UInt8 to Int8 results in error for values equal to or greater than 128.

Run the following code in Playground.

let u1 = UInt8(0x00)
let u2 = UInt8(0x7F)
let u3 = UInt8(0x80)
let u4 = UInt8(0xFF)

let i1 = Int8(u1)
let i2 = Int8(u2)
let i3 = Int8(u3)
let i4 = Int8(u4)

Following this, the system outputs to the console and triggers an error when converting u3 to Int8.

Swift/Integers.swift:3564: Fatal error: Not enough bits to represent the passed value
Check with Playground
Check with Playground

init(truncatingIfNeeded:) succeeds because it converts the type while preserving the bit pattern of the number. For example, 128 in UInt8 and -128 in Int8 are 10000000 in binary.

init(_ source:) occurs of range error because it interprets numeric values. For example, the possible values for Int8 are between -128 and 127. u3 is 128, so it is out of range.

著書紹介

Authored Books

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

Akira Hayashi (林 晃)のアバター Akira Hayashi (林 晃) Representative(代表), Software Engineer(ソフトウェアエンジニア)

アールケー開発代表。Appleプラットフォーム向けの開発を専門としているソフトウェアエンジニア。ソフトウェアの受託開発、技術書執筆、技術指導・セミナー講師。note, Medium, LinkedIn
-
Representative of RK Kaihatsu. Software Engineer Specializing in Development for the Apple Platform. Specializing in contract software development, technical writing, and serving as a tech workshop lecturer. note, Medium, LinkedIn

目次