Java BitSet Example

情到浓时终转凉″ 提交于 2019-11-26 23:59:11

For the specific problem you mentioned: when you called bits2.set(1000001), you set the one millionth and first bit to true. Then when you intersected with bits1, which had the one million, 111 thousand, and 111st bit set, they had no bits in common.

I think what you meant to do was

 bits2.set(0); // set the 0th bit
 bits2.set(6); // set the 6th bit

Does this help clear things up?

If you want to work with bits you can use int values in Java 7.

int bits2 = 0b1000001;
int bits1 = 0b1111111;
bits2 &= bits1;
System.out.println(Integer.toBinaryString(bits2));

prints

1000001
FauxFaux

BitSet doesn't have convenience methods for accepting strings of bits like that. I've provided some below, and now the example works as you'd expect. Note that this uses functionality new in Java 7; it's easy to find implementations of these methods online if you'd like to use Java 6.

import java.util.BitSet;

class Scratch {
    public static void main(String[] args) {
        BitSet bits1 = fromString("1000001");
        BitSet bits2 = fromString("1111111");

        System.out.println(toString(bits1)); // prints 1000001
        System.out.println(toString(bits2)); // prints 1111111

        bits2.and(bits1);

        System.out.println(toString(bits2)); // prints 1000001
    }

    private static BitSet fromString(final String s) {
        return BitSet.valueOf(new long[] { Long.parseLong(s, 2) });
    }

    private static String toString(BitSet bs) {
        return Long.toString(bs.toLongArray()[0], 2);
    }
}

Here are some links about bitSet that would help you:

UPDATE:

In the docs, it is said:

public void set(int bitIndex)

Sets the bit at the specified index to true.

So when you call bits2.set(10);, it is considered as 10 decimal not 1 0 so what you get is the following number 1000000000.

To set it correctly, in this example, I want to set the 2nd bit to 1, so I call bits2.set(1); because the index starts at 0.

In conclusion, for every bit set to 1, you need to call bitSet.Set and provide it with the index of the bit.

I am sharing my implementation for creating a BitSet object using string of bits as input.

private static BitSet createFromString(String s) {
    BitSet t = new BitSet(s.length());
    int lastBitIndex = s.length() - 1;

    for (int i = lastBitIndex; i >= 0; i--) {
        if ( s.charAt(i) == '1'){
            t.set(lastBitIndex - i);                            
        }               
    }

    return t;
}

For string input "1001"

BitSet s1 = createFromString("1001");
    System.out.println(s1);

output :

{0, 3}

Try this:

import java.util.BitSet;

public class BitSetExample {

    public static void main(String args[]){
        BitSet bits1 = new BitSet(7);
        BitSet bits2 = new BitSet(7);

        // set some bits
        for(int i = 0; i < 7; i++) {
            if((i % 2) == 0) bits1.set(i);
            if((i % 3) != 0) bits2.set(i);
        }

        System.out.println("BitSet1: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits1.get(i)? "1 ": "0 ");
        }

        System.out.println("\nBitSet2: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits2.get(i)? "1 ": "0 ");
        }

        System.out.println();

        //And
        bits1.and(bits2);

        System.out.println("b1 = b1 AND b2\nBitSet1: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits1.get(i)? "1 ": "0 ");
        }

        System.out.println();
        System.out.println("BitSet2: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits2.get(i)? "1 ": "0 ");
        }

        System.out.println();

        //Or
        bits1.or(bits2);

        System.out.println("b1 = b1 OR b2\nBitSet1: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits1.get(i)? "1 ": "0 ");
        }

        System.out.println();
        System.out.println("BitSet2: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits2.get(i)? "1 ": "0 ");
        }

        System.out.println();

        //Xor
        bits1.xor(bits2);

        System.out.println("b1 = b1 XOR b2\nBitSet1: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits1.get(i)? "1 ": "0 ");
        }

        System.out.println();
        System.out.println("BitSet2: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits2.get(i)? "1 ": "0 ");
        }

        System.out.println();

        //Setting bits to zero and one
        bits1.set(1);
        bits2.set(1,false);

        System.out.println("set bit 1 of BitSet1 to one and set bit 1 of BitSet2 to zero\nBitSet1: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits1.get(i)? "1 ": "0 ");
        }

        System.out.println();
        System.out.println("BitSet2: ");

        for(int i = 0; i < 7; i++) {
            System.out.print(bits2.get(i)? "1 ": "0 ");
        }

        System.out.println();

    }
}

I hope this is useful. For more information, please visit: https://github.com/m-vahidalizadeh/foundations/blob/master/src/data_structures/BitSetExample.java.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!