2
我看到的算法,例如:爲什麼此算法在Java中使用按位和運算符?
for (int i = 1; i < sums.length; i++) {
/*
* dynamic programming:
* 1. remove a single block from the current subset of blocks
* 2. the corresponding block sum was already calculated
* 3. add the number on the removed block to it
*
* here: always choose the block corresponding
* to the least significant bit of i
*/
int t = Integer.numberOfTrailingZeros(i & -i);
sums[i] = sums[i & ~(1 << t)] + block[t];
//only add block subsets that add up to a face
if (masks.containsKey(sums[i]))
masks.get(sums[i]).add(i);
}
據評論,在該線(INT噸= Integer.numberOfTrailingZeros(I & -i);)撰文表示選擇在一個元件根據數字i的最低有效位分塊數組。
爲什麼作者在i和-i上使用按位和運算符?他們不能只使用我(例如,Integer.numberOfTrailingZeros(i))?
這裏是代碼體形較大的背景下:http://pastebin.com/YB9wsdgD
請將代碼調整爲Java。 –
我只是試圖展示我和-i的樣子。它不打算以任何其他方式有所幫助。我不認爲有Java代碼與網頁中內置的「運行代碼片段」按鈕。 – Tatarize
但將var更改爲int,並將document.write()更改爲System.println()並刪除約
– Tatarize