简体   繁体   English

JAVA BitSet设置

[英]JAVA BitSet setting

I'm developing in Java (JDK 1.8) and manipulating BitSets. 我正在用Java(JDK 1.8)开发并操纵BitSet。 I came accross a strange issue. 我遇到一个奇怪的问题。

I'm instantiating a BitSet of size 160 like: 我正在实例化大小为160的BitSet:

BitSet example = new BitSet(160);

I want to check the size using the size() method that gives the number of bits in the bitset. 我想使用size()方法检查大小,该方法给出了位集中的位数。 In the documentation it is said that the constructor with an int N as parameter is creating a bitset of N bits. 在文档中,据说以int N作为参数的构造函数正在创建N位的位集。

But when I do check the size right after with 但是,当我做后立即检查大小

example.size()

I obtain the value 我获得了价值

192

I do not understand why, does anyone came across this kind of problem ? 我不明白为什么,有人遇到过这种问题吗? link to documentation : http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html 链接到文档: http : //docs.oracle.com/javase/7/docs/api/java/util/BitSet.html

This is because the BitSet constructor creates a BitSet "whose initial size is large enough to explicitly represent" bits in the range given by the parameter. 这是因为BitSet构造函数在参数给定的范围内创建了一个BitSet “其初始大小足以显式表示”位。 So the actual size will be at least the number you give in the parameter, but not necessarily equal to that number. 因此,实际size至少你在参数给出数字,但不一定等于该数字。

The reason it uses 192 in particular is that 192 is a fairly nice binary number: 64 * 3. 它特别使用192的原因是192是一个相当不错的二进制数:64 * 3。

Because the BitSet is actually used long[] to store 0/1 . 因为BitSet实际上用于long []来存储0/1。

private void initWords(int nbits) {
    words = new long[wordIndex(nbits-1) + 1];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM