简体   繁体   English

将BitSet设置为原始类型?

[英]Set a BitSet as a primitive type?

In Java, can you create a BitSet of size 8 and store it as a byte in order to output it? 在Java中,您可以创建大小为8的BitSet并将其存储为字节以便输出吗? The documentation on BitSets doesn't mention it. BitSets上的文档没有提及。 Does that mean no? 那不是吗?

You can't cast BitSet to byte . 您不能将BitSetbyte

You can write code to do what you want though. 您可以编写代码以执行所需的操作。 Given a BitSet named bits , here you go: 给定一个名为bitsBitSet ,您可以执行以下操作:

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. 更新 :上面的代码假定您的位从左到右索引为0到7。 Eg 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. 例如,假设位01101001您认为位0是最左边的0。但是,如果您从右到左分配位,则位0将是最右边的1。在这种情况下,您希望output |= 1 << i

There's nothing built in for that. 没有内置的东西。 You could implement that yourself obviously. 您显然可以自己实现。

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 JVM使用一个32位堆栈单元,即JVM中的每个寄存器存储一个32位地址

we know that primitive boolean is set to be 1 bit but handled as 32 bit. 我们知道原始布尔值设置为1位,但处理为32位。 array of boolean will be considered to be array of bytes 布尔数组将被视为字节数组

in BitSet each component of the bit set has a boolean value 在BitSet中,位集中的每个分量都有一个布尔值

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. 位集合的长度与位集合的逻辑长度有关,并且独立于实现来定义。

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. BitSet类显然不打算将其位导出或导入到本机数据类型,并且如果您只想处理固定大小的单个字节,则也很繁琐。 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: 看来您可能只想使用这样的API:

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 );
    }    
}

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

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