【发布时间】:2014-09-14 04:52:01
【问题描述】:
当我在 swift playground 中使用按位运算符时,147
【问题讨论】:
标签: ios iphone swift bit-manipulation bitwise-operators
当我在 swift playground 中使用按位运算符时,147
【问题讨论】:
标签: ios iphone swift bit-manipulation bitwise-operators
这是 32 位与 64 位整数的问题,因为
左移<< 与其操作数的类型相同。
Playground 使用 64 位架构,因此
147 << 24 = 0x0000000000000093 << 24 = 0x0000000093000000 = 2466250752
在 32 位设备上,147 是一个 32 位 有符号整数,因此
147 << 24 = 0x00000093 << 24 = 0x93000000 = -1828716544
为负数(符号位等于 1)。
但是,结果适合 32 位 无符号 整数,所以这个 在两种架构上都会给出相同的结果:
let x = UInt32(147) << 24 // 2466250752
【讨论】: