BitSet class in Java with example program

BitSet class in Java with example program

In this post we will learn about BitSet class in Java with example programs. BitSet is a convenient class for bit manipulation. The BitSet class can be thought of as a vector of bits that grows in size as needed. Remember that a bit can either be 1(true) or 0(false). Each component of the bit set has a boolean value. The bits of a BitSet can be indexed by positive integer starting from 0 (like indexing an array).

BitSet class provides  method to get, set, or clear individual bits. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations. By default, all bits in the set initially have the value false(0).

The BitSet class is not thread safe, so you may need external synchronization for use in a multithreaded environment.

The BitSet Class

The BitSet is a class defined in the java.util package. The BitSet defines the following two constructors.

  • BitSet( ) – Creates an empty BitSet object.
  • BitSet(int nbits) – Creates a BitSet object with number of bits(initial capacity) as mentioned by nbits.

BitSet Methods

The BitSet class also provides methods for common bit manipulation using bitwise logical AND, bitwise logical OR, and bitwise logical exclusive OR. Following are some of the notable methods defined in the class.

  • boolean get(int bitIndex)  Returns the current state of the bit(true or false) at the specified index.
  • BitSet get(int fromIndex,int toIndex) – Returns a new BitSet composed of bits from the invoking BitSet from fromIndex (inclusive) to toIndex (exclusive).
  • void set(int bitIndex) – Sets the bit at the specified index to true.
  • void set(int bitIndex, boolean value) – Sets the bit at the specified index to the specified value.
  • void set(int fromIndex, int toIndex) – Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true.
  • int cardinality() – Returns the number of bits set to 1 (true) in the invoking BitSet.
  • void clear() – Sets all of the bits in the BitSet to false.
  • void clear(int bitIndex) – Sets the bit specified by the index to false.
  • void clear(int fromIndex, int toIndex) – Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to false.
  • void flip(int bitIndex) – Reverses the value of the bit at the specified index.
  • void flip(int fromIndex, int toIndex) – Reverses the value of the bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive).
  • void and(BitSet set) – Performs a logical AND on the invoking bit set with the argument bit set. The invoking bit set is modified to hold the result of the operation.
  • void or(BitSet set) – Performs a logical OR on the invoking bit set with the argument bit set. The invoking bit set is modified to hold the result of the operation.
  • void xor(BitSet set) – Performs a logical XOR on the invoking bit set with the argument bit set. The invoking bit set is modified to hold the result of the operation.
  • void andNot(BitSet set) – Clears all of the bits in the invoking BitSet whose corresponding bit is 1(true) in the argument BitSet.
  • String toString() – Returns a string representation of the invoking bit set. For every index for which the BitSet contains a 1(true), the decimal representation of that index is included in the result.

BitSet example Java program

The following program illustrates the usage of various methods discussed above.

Below is the output of running the above program.

Note that operations like AND, OR, XOR, clear, flip etc modifies the invoking object. Thus, in the above program, we have created a copy of original bits using a temp BitSet object. Also when you print the BitSet object using System.out.println, it calls the toString() method which returns all the index at which the BitSet contains 1(true).

Hope you understood the usage of BitSet class in Java. If you have any doubt, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.