How to make BitSet to be not simplified [JAVA]

Marc43 :

I am using BitSet to represent possible hours where lectures are filled in, the case is that when you put to false the corner bits, they are simplified, this means that they are not anymore in the BitSet. How can I ask BitSet to not simplify?

To make my explanation clearer this is the code:

   for(Map.Entry<GrupAssig, BitSet> entry : bitsetPerGrup.entrySet()){

            BitSet bitset = entry.getValue();

            //n franges per dia
            int numFranges = UnitatDocent.getNumFranges();
            int indexDia = this.dia.id() * numFranges;

            bitset.clear(indexDia, indexDia+numFranges);
     }

Imagine that the bitset has 60 bits by default, and numFranges=12 and this.dia.id()=4. This would make the last twelve bits set to 0. The result I get is:

111111111111111111111111111111111111111111111111

But if this.dia.id()=3 I get:

11111111111111111111111111111111111100000000000011111111111

And you can print the BitSet this way:

    public static void printBitset(BitSet b) {
        StringBuilder s = new StringBuilder();
        for( int i = 0; i < b.length();  i++ )
        {
            s.append( b.get( i ) == true ? 1 : 0 );
        }

        System.out.println( s );
    }

Which demonstrates what I am saying.

Thank you.

Ryan Cogswell :

Here's the documentation for BitSet.length:

length()
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one.

If you need to print out a certain number of bits (e.g. 60) then use a constant instead of ".length()" for your loop. You can call ".get(index)" on any index regardless of the length and it will give you the result for that bit.

For instance the following code produces "0000011000":

import java.util.BitSet;

public class Main {

    public static void main(String[] args) {
        BitSet bits = new BitSet();
        bits.set(5);
        bits.set(6);
        StringBuilder bitString = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            bitString.append(bits.get(i) ? "1" : "0");
        }
        System.out.println(bitString.toString());
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=96821&siteId=1