我正在研究一個簡單的組合部分,並發現我需要恢復兩位給定位置的其他兩位在4位srring中的位置。在一組四個中給出兩個位,找到另外兩個位的位置
例如,(0,1)映射到(2,3),(0,2)到(1,3)等,總共六個組合。
我的解決方案是採用四個嵌套三元運營商測試位:
ab is a four bit string, with two bits set.
c = ((((ab & 1) ? (((ab & 2) ? ...))) : 0)
abc = ab | c
recover the last bit in the same fashion from abc.
我要澄清,而無需使用for循環,我的目標語言是C++的元編程模板。我知道我明確指定了語言,但在我看來,它仍然是不可知的
你能想出更好的方法/更聰明的方法嗎? 謝謝
吧,我在某種位/遞歸方法感興趣,雖然 – Anycorn 2010-03-23 21:53:49