java - 为什么 Java BitSet 被打包成 6 个字节?
解决方案
我假设您正在询问 JDK 中的这段代码:
private static int wordIndex(int bitIndex) {
return bitIndex >> ADDRESS_BITS_PER_WORD; // ADDRESS_BITS_PER_WORD is 6, question 1
}
public BitSet(int nbits) {
// nbits can't be negative; size 0 is OK
if (nbits < 0)
throw new NegativeArraySizeException("nbits < 0: " + nbits);
initWords(nbits);
sizeIsSticky = true;
}
private void initWords(int nbits) {
words = new long[wordIndex(nbits-1) + 1]; // question 2
}
initWords
初始化 along[]
以支持位,本质上将位存储到 64 位的“字”中。请注意,这似乎是一个实现细节。这应该多长时间long[]
?好吧,它应该是最后一个单词的单词索引+ 1,因为索引是从零开始的。
最后一个词的索引是多少?好吧,该wordIndex
方法可以告诉我们一个位的单词索引,所以如果我们给它最后一位的索引,nbits - 1
(同样因为索引是从零开始的),它会给我们想要的。这应该回答你的第二个问题。
如何wordIndex
找到单词索引?嗯,a 中有 64 位long
,所以我们只需要将 a 除以bitIndex
64。除以 64 的另一种方法是什么?左移 6 次,因为 64 = 2 的 6 次方。有关更多信息,请参阅此帖子。
推荐阅读
- android - 尝试在空对象引用“''''上调用虚拟方法 'java.lang.String java.lang.Object.toString()' 试图压缩图像
- javascript - 未捕获的 ReferenceError:请求未在 HTMLAnchorElement.onclick 中定义
- object - 在目标 c 中实现具有模糊效果的视频中的视频
- java - 解压嵌套的jar文件java
- android - 如何从 json api 在网格视图中显示数据
- typescript - 从子类增强基类方法的最佳方法是什么
- python - 如何生成最多 10 次迭代的随机折线图?
- ios - 在组织内外分发 iOS 应用程序
- dart - 将芯片添加到材质文本字段
- angular5 - 如何将另一个事件绑定到angular5中甜蜜警报2模式中的关闭按钮