如何在Swift中使用两个UInt32
数字表示UInt16
,反之亦然?
例如,UInt32
65536应该是(UInt16)1和(UInt16)65535,而(UInt16)0和(UInt16)65533组合到(UInt32)65533
答案 0 :(得分:3)
简单的按位操作
var number1: UInt16 = 1
var number2: UInt16 = 65535
let number: UInt32 = UInt32(number1) << 16 | UInt32(number2)
number1 = UInt16(number >> 16)
number2 = UInt16(number & 0xFFFF)
答案 1 :(得分:1)
func toInt16(value:UInt32)->(UInt16,UInt16){
return (UInt16(value >> 16),UInt16(value & UInt32(UInt16.max)))
}
func toInt32(value1:UInt16,value2:UInt16)->UInt32{
return (UInt32(value1) << 16 | UInt32(value2))
}