将 BitSet 设置为原始类型?

发布于 2024-11-01 19:17:24 字数 72 浏览 1 评论 0原文

在Java中,您可以创建一个大小为8的BitSet并将其存储为字节以便输出吗? BitSets 的文档没有提到它。这是否意味着不?

In Java, can you create a BitSet of size 8 and store it as a byte in order to output it? The documentation on BitSets doesn't mention it. Does that mean no?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿嘿嘿 2024-11-08 19:17:24

您无法将 BitSet 转换为 byte

不过,您可以编写代码来执行您想要的操作。给定一个名为 bitsBitSet,如下所示:

byte output = 0;
for (int i = 0; i < 8; i++) {
    if (bits.get(i)) {
        output |= 1 << (7 - i);
    }
}

更新:上面的代码假设您的位从左到右索引为 0 到 7。例如,假设位 01101001 您认为位 0 是最左边的 0。但是,如果您从右到左分配位,那么位 0 将是最右边的 1。在这种情况下,您需要 输出|= 1 <<我代替。

You can't cast BitSet to byte.

You can write code to do what you want though. Given a BitSet named bits, here you go:

byte output = 0;
for (int i = 0; i < 8; i++) {
    if (bits.get(i)) {
        output |= 1 << (7 - i);
    }
}

Update: The above code assumes that your bits are indexed 0 to 7 from left to right. E.g. assuming the bits 01101001 you consider bit 0 to be the leftmost 0. If however you're assigning the bits from right to left then bit 0 would be the rightmost 1. In which case you want output |= 1 << i instead.

哭了丶谁疼 2024-11-08 19:17:24

没有为此内置任何东西。显然你可以自己实现。

There's nothing built in for that. You could implement that yourself obviously.

攀登最高峰 2024-11-08 19:17:24

位集是

JVM 使用 32 位堆栈单元的位数组,即 JVM 中的每个寄存器存储一个 32 位地址,

我们知道原始布尔值设置为 1 位,但按 32 位处理。布尔数组将被视为

BitSet 中的字节数组,位集的每个组成部分都有一个布尔值

每个位集都有一个当前大小,
这是空间的位数
当前由位组使用。笔记
大小与
位集的实现,所以它可能
随实施而变化。长度
位组的大小与逻辑长度相关
位组的并被定义
独立于实现。

bit set is an array of bits

the JVM uses a 32-bit stack cell ie each register in the JVM stores one 32-bit address

we know that primitive boolean is set to be 1 bit but handled as 32 bit. array of boolean will be considered to be array of bytes

in BitSet each component of the bit set has a boolean value

Every bit set has a current size,
which is the number of bits of space
currently in use by the bit set. Note
that the size is related to the
implementation of a bit set, so it may
change with implementation. The length
of a bit set relates to logical length
of a bit set and is defined
independently of implementation.

末骤雨初歇 2024-11-08 19:17:24

BitSet 类显然不是为了将其位导出或导入到本机数据类型,并且如果您只想处理单个字节的固定大小,那么它也相当繁重。因此,如果您只想独立操作字节的位然后使用结果字节,那么它可能不是您所需要的。看来您可能只想使用这样的 API:

SimpleBitSet bs = new SimpleBitSet( 'A' );
bs.setBit( 5 );
byte mybyte = bs.getByte();

因此,这种简化的位集的实现可能如下所示:

public class SimpleBitSet
{
    private byte bits;

    public SimpleBitSet( int bits )
    {
        this.bits = (byte) bits;
    }

    public byte getByte()
    {
        return bits;
    }

    public boolean getBit( int idx )
    {
        checkIndex( idx );
        return ( bits & ( 1 << idx ) ) != 0;
    }

    public void setBit( int idx )
    {
        checkIndex( idx );
        bits |= 1 << idx;
    }

    public void clearBit( int idx )
    {
        checkIndex( idx );
        bits &= ~( 1 << idx );
    }

    protected void checkIndex( int idx )
    {
        if( idx < 0 || idx > 7 )
            throw new IllegalArgumentException( "index: " + idx );
    }    
}

The BitSet class is obviously not intended to export or import its bits to native datatypes and also quite heavy if you just want to deal with the fixed size of a single byte. It might thus not be what you need if you just want to manipulate the bits of a byte independently and then use the resulting byte. It seems you might just want to use a API like this:

SimpleBitSet bs = new SimpleBitSet( 'A' );
bs.setBit( 5 );
byte mybyte = bs.getByte();

So a implementation of such a simplified bit set could look like this:

public class SimpleBitSet
{
    private byte bits;

    public SimpleBitSet( int bits )
    {
        this.bits = (byte) bits;
    }

    public byte getByte()
    {
        return bits;
    }

    public boolean getBit( int idx )
    {
        checkIndex( idx );
        return ( bits & ( 1 << idx ) ) != 0;
    }

    public void setBit( int idx )
    {
        checkIndex( idx );
        bits |= 1 << idx;
    }

    public void clearBit( int idx )
    {
        checkIndex( idx );
        bits &= ~( 1 << idx );
    }

    protected void checkIndex( int idx )
    {
        if( idx < 0 || idx > 7 )
            throw new IllegalArgumentException( "index: " + idx );
    }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文