开发者

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

开发者 https://www.devze.com 2023-02-16 09:31 出处:网络
BitSet bits1 = new BitSet(00101010); System.out.println(bits1); Question : Why the output is returning { }.
  • 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 Bit开发者_开发百科Set(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. 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:

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):

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()

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, 6, 7, 10, 12, 13, 21, 23, 24, 26, 29}


From the BitSet documentation:

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.

You need to initialise the BitSet properly (e.g. for 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. You need to set the bits like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消