我已经设置了以下使用 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 的 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.
如上所示,顺序是“从低到高”。这意味着首先是最低有效位(个位),最后是最高有效位。
排序(符号顺序从左到右或数字顺序从最少到最多)都有意义,尽管方式不同。
关于java - BitSet 向后显示值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166056/