范围内值的位操作

时间:2017-05-23 14:11:31

标签: swift operators bit-manipulation bitwise-operators binary-operators

我有一个选项设置,掩码applicationReserved = 0x0F000000指示4位范围。由此,我想生成可能的值0x010000000x020000000x03000000,...

我已经提出了一些可能的解决方案,但我怀疑可能有更明确的表达方式:

applicationReserved & -applicationReserved
applicationReserved & -applicationReserved << 1
...

applicationReserved / 15
applicationReserved / 15 * 2 
...

1 个答案:

答案 0 :(得分:0)

我认为这与您在评论中描述的内容最为接近:

let x = 0x0F0000000
var offset = 0

while offset < 63 && (x & (1 << offset)) == 0 { offset += 1 }

for i in 0..<16 {
    let y = i << offset
    print(String(y, radix: 16))
}