BitSet之为什么用long保存信息
BitSet內(nèi)部使用long[] words來保存位信息。咋看之下并不理解原因,在解讀set(int bitIndex)之后似乎有了一些領(lǐng)悟。
public void set(int bitIndex) {
if (bitIndex < 0)
throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
//用來計算應(yīng)將bitIndex對應(yīng)保存到哪個位置。long占用4個字節(jié),64位(2的6次方),因此計算bitIndex對應(yīng)著long數(shù)組的第幾位。
int wordIndex = wordIndex(bitIndex);
//判斷是否需要擴容。
expandTo(wordIndex);
//保存信息,將開關(guān)信息寫入對應(yīng)long的位信息當(dāng)中。
words[wordIndex] |= (1L << bitIndex); // Restores invariants
checkInvariants();
}
private static int wordIndex(int bitIndex) {
return bitIndex >> ADDRESS_BITS_PER_WORD;
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/winnie-sr/p/3501712.html
總結(jié)
以上是生活随笔為你收集整理的BitSet之为什么用long保存信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 平安银行信用卡还款方式有哪些?这几招让你
- 下一篇: delphi xe4 ini文件不能读取