I’ve set up the following code running with 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.
That makes me wonder why would a BitSet show the indexes backward. Is there any rational explanation?
Quoting from the Java standard for
BitSet: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.
Either ordering (notational order left-to-right or numeric order least-to-most) would make sense, albeit in a different way.