简体   繁体   English

BitSet向后显示值?

[英]BitSet shows up values backward?

I've set up the following code running with Java: 我已经设置了以下使用Java运行的代码:

BitSet bitSet = BitSet.valueOf(new byte[] { (byte)15 });
System.out.println(bitSet);

which to my surprise prints 令我惊讶的打印

{0, 1, 2, 3} //the indexes of the 1's in the bitset

instead of 代替

{ 4, 5, 6, 7 }.

15 in 2's complement is written as 00001111 (with 1 byte), if I am not mistaken. 如果我没有弄错的话,15 in 2的补码写成00001111(带1个字节)。

That makes me wonder why would a BitSet show the indexes backward. 这让我想知道为什么BitSet会向后显示索引。 Is there any rational explanation? 有没有合理的解释?

Quoting from the Java standard for 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. 对于此BitSet包含设置状态位的每个索引,该索引的十进制表示包含在结果中。 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. 这些索引按照从最低到最高的顺序列出,用“,”(逗号和空格)分隔并用大括号括起来,从而产生一组整数的通常数学符号。

As this says, the order is "from lowest to highest". 如上所述,订单是“从最低到最高”。 This means the least significant bit (the ones bit) first, and the most significant bit last. 这意味着首先是最低有效位(1位),最后是最高有效位。

Either ordering (notational order left-to-right or numeric order least-to-most) would make sense, albeit in a different way. 订购(从左到右的符号顺序或从最少到最大的数字顺序)是有意义的,尽管是以不同的方式。

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

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