Java BitSet Example

JavaBitset

Java Problem Overview


I'm looking for a good Java BitSet example to work with 0 and 1s. I tried looking at the Javadocs but I don't understand the usage of the class by just reading that. For instance, how would the and, or, and xor methods work on two different BitSet objects?

For example:

  BitSet bits1 = new BitSet();
  BitSet bits2 = new BitSet();
        
  bits2.set(1000001);
  bits1.set(1111111);
      
  bits2.and(bits1);
         
  System.out.println(bits2);

If I do this it returns bits2 as empty why is that?

Java Solutions


Solution 1 - Java

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 - Java

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

Solution 3 - Java

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);
	}
}

Solution 4 - Java

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.

Solution 5 - Java

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}

Solution 6 - Java

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.

Solution 7 - Java

In case you also need to work with bit addresses longer than Integer.MAX_VALUE I modified java.util.BitSet (here: LongBitSet) to accept long inputs in get and set methods. I did it to fully use memory for a prime sieve experiment.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteffan HarrisView Question on Stackoverflow
Solution 1 - JavaLouis WassermanView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - JavaFauxFauxView Answer on Stackoverflow
Solution 4 - JavaAdel BoutrosView Answer on Stackoverflow
Solution 5 - JavaAditya GaykarView Answer on Stackoverflow
Solution 6 - JavaMohammadView Answer on Stackoverflow
Solution 7 - JavajuanmfView Answer on Stackoverflow