[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/AllAlgorithms/java/master/bit-manipulation/BooleanBitSet.java [Back]  [Original]

/**
 * BooleanBitSet that encodes bits in longs, thus saving memory compared to a
 * boolean[] array.
 */
public class BooleanBitSet {

    final long[] storage;

    public BooleanBitSet(int size) {
        storage = new long[(int)Math.ceil(size / 64.0)];
    }

    public void set(int adress, boolean value){

        // translate adress to holding long
        int pos = (int)Math.floor(adress/ 64.0);
        long holder = storage[pos];
        int offset = adress % 64;

        if(value){
            holder |= 1  offset) & 1) == 1;
    }

    public static void main(String...strings){

        BooleanBitSet b = new BooleanBitSet(20);
        b.set(0, true);
        b.set(1, true);
        b.set(3, true);
        b.set(5, true);

        System.out.println(b.get(0));
        System.out.println(b.get(1));
        System.out.println(b.get(2));
        System.out.println(b.get(3));
        System.out.println(b.get(4));
        System.out.println(b.get(5));
        System.out.println(b.get(6));
    }


}

Web Proxy Viewer  |  New URL  |  Original Page