In C we can declare unions as follows:
1 2 3 4 |
union Data { int i; uint u; } data; |
So the same storage is used for the int and uint.
We can achieve something similar in Swift using bitPattern.
1 2 3 4 5 |
let a: Int8 = 1 let b = UInt8(bitPattern: a) let negativeA: Int8 = -1 let bFromNegative = UInt8(bitPattern: negativeA) |
In the above example b is 1 and bFromNegative is 255 (the two complement of -1).
UInt8(bitPattern: … initialiser will use the same bit storage.