简体   繁体   English

BitSet valueOf是什么?

[英]BitSet valueOf does what?

I'm having an extremely difficult time understand BitSet.valueOf(bytearray) 我很难理解BitSet.valueOf(bytearray)

I have the following code: 我有以下代码:

byte[] a = new byte[]{(byte) 0x2D, (byte) 0x04};
//binary => 0010 1101 0000 0100
BitSet bs = BitSet.valueOf(a);
System.out.println(bs);

Code above gives me an output of {0, 2, 3, 5, 10} . 上面的代码为我提供了{0, 2, 3, 5, 10} Why? 为什么?

I thought it was supposed to return the indices that are true, or holds 1, backwards which then should be {2, 8, 10, 11, 13} . 我以为应该返回正确的索引,或者保持1,然后向后返回{2, 8, 10, 11, 13}

As you would expect, BitSet is doing the right thing. 如您所料, BitSet做正确的事。 You seem to be misinterpreting which bit is zero and which one is seven. 您似乎在误解哪个位为零,哪个位为7。 For the first byte, your binary representation is correct, but remember that the first bit is on the right (lowest to highest, as a weird artifact of how we write numbers): 对于第一个字节,您的二进制表示形式是正确的,但是请记住,第一位在右边(从低到高,这是我们如何写数字的怪异产物):

Bit Value:  0 0 1 0 1 1 0 1
Index:      7 6 5 4 3 2 1 0

Reading off the indices gives 0, 2, 3, 5, 10 读取索引得出0, 2, 3, 5, 10

It is the first byte from LSB to MSB followed by the second byte from LSB to MSB: 它是从LSB到MSB的第一个字节,然后是从LSB到MSB的第二个字节:

1011 0100 0010 0000
| ||  |     |
0 23  5     10

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

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