BitSet 有时会丢失 Bits

huangapple go评论69阅读模式
英文:

BitSet sometimes drops Bits

问题

我目前正在尝试从Java中的文件中读取单个位。

我将数据读入字节数组,然后将字节数组转换为BitSet。

问题是有时在转换后的BitSet中会丢失一些位。

在以下示例中,我有2个字节数组,每个数组都有25个非常相似的字节。但其中一个被转换为预期的200位,而另一个只有197位,我不知道为什么。

import java.nio.ByteBuffer;
import java.util.BitSet;

public class Main {
    public static void main(String[] args) {
        ByteBuffer cb = ByteBuffer.wrap(new byte[] {(byte)0x18,(byte)0x8C,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,});
        BitSet cBits = BitSet.valueOf(cb);
        System.out.println(cBits.length());
        ByteBuffer db = ByteBuffer.wrap(new byte[] {(byte)0x17,(byte)0x8c,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x8c,(byte)0x8c,});
        BitSet dBits = BitSet.valueOf(db);
        System.out.println(dBits.length());
    }
}

Doodle

英文:

I am currently trying to read a single bit from a File in Java.

I read the Data into a byte array and then convert the byte array into a Bitset.

The Problem is that sometimes there are a few bits missing in the converted Bitset.

In the following Example i have 2 Byte Arrays, each with 25 very similar Bytes. But one is converted into the expected 200 bits and the other one into only 197 bits and i have no idea why.

import java.nio.ByteBuffer;
import java.util.BitSet;
public class Main {
public static void main(String[] args) {
ByteBuffer cb = ByteBuffer.wrap(new byte[] {(byte)0x18,(byte)0x8C,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,});
BitSet cBits = BitSet.valueOf(cb);
System.out.println(cBits.length());
ByteBuffer db = ByteBuffer.wrap(new byte[] {(byte)0x17,(byte)0x8c,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x18,(byte)0x8c,(byte)0x8c,});
BitSet dBits = BitSet.valueOf(db);
System.out.println(dBits.length());
}
}

Doodle

答案1

得分: 2

Java位集合的length()是最高设置位的结果,而不是传递的数组的长度。这与位集合描述为根据需要增长的数据结构一致——它的逻辑大小是其设置位的结果,而不是某个底层缓冲区的物理容量。

返回此位集合的“逻辑大小”:位集合中最高设置位的索引加一。如果位集合不包含设置位,则返回零。

您的第一个示例中,最高有效字节为0x18,或二进制00011000。与第二个位集合相比,第一个位集合有三个前导零位,这解释了差异,第二个位集合的最高有效字节为0x8c(10001100)。

英文:

The length() of a Java bitset is a consequence of the most significant set bit, not the length of the array which was passed. This is consistent with the bitset's description as a data structure which grows as needed--its logical size is a consequence of its set bits, not the physical capacity of some underlying buffer.

> Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one. Returns zero if the BitSet contains no set bits.

Your first example has the most significant byte as 0x18, or binary 00011000. The three leading zero bits account for the discrepancy when compared with the second bitset, whose most significant byte is 0x8c (10001100)

huangapple
  • 本文由 发表于 2020年8月14日 08:03:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63404762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定