Java BitSet Example

Solution 1:

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?

Solution 2:

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