[英]BitSet from byte[] with strange lenght
my code is : 我的代码是:
String blah = "blah";
byte[] blahBytes = blah.getBytes("US-ASCII");
System.out.println(Arrays.toString(blahBytes));
BitSet set = BitSet.valueOf(blahBytes);
System.out.println(set.length());
the output is : 输出为:
[98, 108, 97, 104]
31
Why is length()
returning 31? 为什么
length()
返回31? Shouldn't it be 32? 不是32岁吗?
Bit set length is determined by the position of the highest bit set to 1
. 位设置长度由设置为
1
的最高位的位置确定。 Since all bytes that you pass to construct bit set represent ASCII character subset of UNICODE, the 8-th bit is always zero. 由于传递给构造位集的所有字节均表示UNICODE的ASCII字符子集,因此第8位始终为零。 Therefore, the highest bit set to
1
will be either bit 30 or bit 31, depending on the letter or digit in the end of your string: if you pass "bla1"
instead of "blah"
you would get 30 ( demo 1 ). 因此,根据字符串末尾的字母或数字,设置为
1
的最高位将是30位或31位:如果传递"bla1"
而不是"blah"
,则将获得30( 演示1 )。 If you use control characters, such as <TAB>
you could get an even shorter bit set of 28 ( demo 2 ). 如果使用控制字符(例如
<TAB>
,则可以得到更短的28位( 演示2 )。
If you would like to get a length rounded up to the next multiple of 8, use 如果您希望将长度四舍五入为8的下一个倍数,请使用
int roundedLength = 8 * ((set.length() + 7) / 8);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.