【发布时间】:2017-04-02 18:21:32
【问题描述】:
考虑以下代码(java8):
@Test
public void testBigIntegerVsBitSet() throws Throwable
{
String bitString529 = "00000010 00010001"; // <- value = 529 (LittleEndian)
byte[] arr529 = new byte[] { 0x02, 0x11 }; // <- the same as byte array (LittleEndian)
BigInteger bigIntByString = new BigInteger( bitString529.replace( " ", ""), 2); // throws if there is a blank!
BigInteger bigIntByArr = new BigInteger( arr529);
BitSet bitsetByArr = BitSet.valueOf( arr529); // interpretes bit-order as LittleEndian, but byte-order as BigEndian !!!
System.out.println( "bitString529 : " + bitString529); // bitString529 : 00000010 00010001
System.out.println( "arr529.toString : " + Arrays.toString( arr529)); // arr529.toString : [2, 17]
System.out.println( "bigIntByString : " + bigIntByString); // bigIntByString : 529
System.out.println( "bigIntByArr : " + bigIntByArr); // bigIntByArr : 529
System.out.println( "bitsetByArr : " + bitsetByArr.toString() ); // bitsetByArr : {1, 8, 12}
System.out.println( "expecting : {0, 4, 9}"); // expecting : {0, 4, 9}
String bigIntByStringStr = toBitString( bigIntByString::testBit);
String bigIntByArrStr = toBitString( bigIntByArr::testBit);
String bitsetByArrStr = toBitString( bitsetByArr::get);
System.out.println( "bigIntByStringStr: " + bigIntByStringStr); // bigIntByStringStr: 1000100001000000
System.out.println( "bigIntByArrStr : " + bigIntByArrStr); // bigIntByArrStr : 1000100001000000
System.out.println( "bitsetByArrStr : " + bitsetByArrStr ); // bitsetByArrStr : 0100000010001000
}
private String toBitString( Function<Integer, Boolean> aBitTester)
{
StringBuilder sb = new StringBuilder();
for ( int i = 0; i < 16; i++ )
{
sb.append( aBitTester.apply( i) ? "1" : "0");
}
return sb.toString();
}
这证明了 BitSet 将字节数组解析为 BIG_ENDIAN,而将(单个字节的)位顺序解释为 LITTLE_ENDIAN。相比之下,BigInteger 在 LITTLE_ENDIAN 中解释两者,即使由位字符串加载也是如此。
尤其是对两个类的位索引(BitInteger::testBit vs. BitSet::get)的迭代会产生不同的结果。
这种不一致有什么原因吗?
【问题讨论】:
标签: java endianness