我设置了一个由掩码 applicationReserved = 0x0F000000
指示的 4 位范围的选项。由此,我想生成可能的值 0x01000000
、0x02000000
、0x03000000
、...
我已经想出了一些可能的解决方案,但我怀疑可能有比这更清晰的表达:
applicationReserved & -applicationReserved
applicationReserved & -applicationReserved << 1
...
或
applicationReserved / 15
applicationReserved / 15 * 2
...
最佳答案
我认为这最接近您在评论中描述的内容:
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))
}
关于swift - 对范围内的值进行位操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44137204/