我已经设置了以下使用Java运行的代码:
BitSet bitSet = BitSet.valueOf(new byte[] { (byte)15 });
System.out.println(bitSet);
令我惊讶的打印
{0, 1, 2, 3} //the indexes of the 1's in the bitset
代替
{ 4, 5, 6, 7 }.
如果我没有弄错的话,15 in 2的补码写成00001111(带1个字节).
这让我想知道为什么BitSet会向后显示索引.有没有合理的解释?
解决方法:
引用BitSet的Java标准:
Returns a string representation of this bit set. For every index for
which this BitSet contains a bit in the set state, the decimal
representation of that index is included in the result. Such indices
are listed in order from lowest to highest, separated by “, ” (a comma
and a space) and surrounded by braces, resulting in the usual
mathematical notation for a set of integers.
如上所述,订单是“从最低到最高”.这意味着首先是最低有效位(1位),最后是最高有效位.
订购(从左到右的符号顺序或从最少到最大的数字顺序)是有意义的,尽管是以不同的方式.