简体   繁体   English

如何将值传递给BitSet类并创建包含BitSet值的列表?

[英]How to pass values to BitSet Class and create list containing BitSet Values?

  •  BitSet bits1 = new BitSet(00101010); System.out.println(bits1); 

Question : Why the output is returning { }. 问题:为什么输出返回{}。

  •  List<BitSet> list = new ArrayList<BitSet>(); list.add(new BitSet(00010010)); list.add(new BitSet(10000001)); list.add(new BitSet(01000001)); System.out.println(test.orTogether(list)); 

Not able to pass values to the method. 无法将值传递给方法。 As the list contains [{ }, { }, { }]. 由于列表包含[{},{},{}]。

The problem is that the constructor public BitSet(int) doesn't take the bits, but the number of bits you want to store. 问题在于构造函数public BitSet(int)不会占用位数,而是要存储的位数。 So, the content is still empty... 因此,内容仍然为空...

To set the bits, you can create an extra method: 要设置位,可以创建一个额外的方法:

public BitSet createBitSet(String bits)
{
    int len = bits.length();
    BitSet bs = new BitSet(len);
    for (int i = 0; i < len; i++)
    {
        bs.set(len - i - 1, bits.charAt(i) == '1');
    }
    return bs;
}

And then: 接着:

list.add(createBitSet("00010011"));

(I didn't test the method, so they may be some errors...) (我没有测试该方法,所以它们可能有一些错误...)

To construct your BitSet objects you probably want to use a method similar to: 要构造您的BitSet对象,您可能希望使用类似于以下方法:

static BitSet createBitSetFromString( String s ) {
  BitSet ret = new BitSet( s.length );
  for( int i = 0 ; i < s.length() ; i++ ){
    if( s.charAt( i ) == '1' ) {
      ret.set( s.length() - 1 - i ) ;
    }
  }
  return ret ;
}

then, instead of 然后,代替

BitSet bits1 = new BitSet(00101010); 

You can do: 你可以做:

BitSet bits1 = createBitSetFromString( "00101010" ) ; 

Here is a simple factory method to create a BitSet from a binary input String (I copied it from this previous answer of mine ): 这是一个简单的工厂方法,用于从二进制输入String创建一个BitSet (我从mine的上一个答案中复制了它):

public static BitSet createBitset(final String input){
    final int length = input.length();
    final BitSet bitSet = new BitSet(length);
    for(int i = length - 1; i >= 0; i--){
        // anything that's not a 1 is a zero, per convention
        bitSet.set(i, input.charAt(i) == '1');
    }
    return bitSet;
}

You can also use it with int or long bit masks by using toBinaryString() 您还可以通过使用toBinaryString()将其与intlong位掩码一起使用

int someInt = 1234567;
long someLong = 1234567890L;
BitSet bitSetFromInt =
    createBitset(Integer.toBinaryString(someInt));
BitSet bitSetFromLong =
    createBitset(Long.toBinaryString(someLong));
System.out.println(bitSetFromInt);
System.out.println(bitSetFromLong);

Output: 输出:

{0, 3, 5, 6, 8, 10, 11, 13, 18, 19, 20} {0、3、5、6、8、10、11、13、18、19、20}
{0, 3, 6, 7, 10, 12, 13, 21, 23, 24, 26, 29} {0,3,6,7,10,12,13,21,23,24,26,29}

From the BitSet documentation : BitSet文档中

BitSet(int nbits) Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1. BitSet(int nbits)创建一个位集合,其初始大小足够大,以明确表示索引范围为0到nbits-1的位。

You need to initialise the BitSet properly (eg for 01001 ): 您需要正确初始化BitSet(例如01001 ):

BitSet bs = new BitSet(5);
bs.set(0, true);
bs.set(3, true);

The BitSet constructor takes an int which is the initial size, not a binary number. BitSet构造函数采用一个int值,该值是初始大小,而不是二进制数。 You need to set the bits like this: 您需要像这样设置位:

bitset.set(2);
bitset.set(4);
bitset.set(6);

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

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