0

我有一個選項設置與掩碼applicationReserved = 0x0F000000指示的4位範圍。由此,我想生成的可能值0x010000000x020000000x03000000,...範圍內的值的位操作

我已經想出一些可能的解決辦法,但我懷疑有可能是一個更清晰的表達莫過於:

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

applicationReserved/15 
applicationReserved/15 * 2 
... 
+0

一個範圍必須有一個開始和結束。你只有一個綁定 –

+1

我不明白這個問題,我不明白解決方案。鑑於你似乎描述的一組可能的值,你的意思是任何'x'這樣'(x&applicationReserved)!= 0'被保留了嗎?如果是這樣,「解決方案」甚至意味着什麼 - 你想測試這種情況嗎?生成它們?你在做什麼 – harold

+0

在這個特定的例子中,我認爲它只是假設它是一個4位的範圍。 'applicationReserved = 0x0F000000'表示'0x010000000 - 0x0F0000000'。下面是這個特定實現的[apple docs](https://developer.apple.com/reference/uikit/uicontrolevents/uicontroleventapplicationreserved?language=objc)。 – gmcerveny

回答

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)) 
}