swift - 对范围内的值进行位操作

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

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

我已经想出了一些可能的解决方案,但我怀疑可能有比这更清晰的表达:

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/

相关文章:

ios - 命令行 C 程序到 Swift

swift - 在命令行工具上使用 Swift URLSession 示例代码

c - C 中的运算符 &= 和 ~1U

c - 查找位数组中设置的最高有效位(最左边)

c - 从 A 中提取某些位并仅替换 B 中某个位置处提取的那些位

ios - 处理来自 continueUserActivity 方法的 Firebase 邀请

ios - 如何在 UITableViewCell 内实现动态大小的 UICollectionView?

openerp - Openerp/Odoo 中有哪些可用的域运营商?

javascript - 为什么使用processing.js从值中减去-0.2会将整数变成无理数?

bit-manipulation - 通过 SIMD 查找数组中元素的索引。一个快速的方法