Reader Level:
ARTICLE

Working With BitSet Class in Java

Posted by Abhishek Dubey Articles | Java June 04, 2012
In this article we are going to describe one of the important classes of the util package in Java. So we explain the use of the BitSet class test; also what is the need of the bit class.
  • 0
  • 0
  • 5351

Introduction

In this article we are going to describe one of the important classes of the util package in Java. So we explain the use of the BitSet class test; also what is the need of the bit class. A BitSet class creates a special type of array that holds bit values. This array can increase in size as needed. This makes it similar to a vector of bits.

BitSet Class

The BitSet class implements a vector of bits that can grow as needed. Each component of the bit set has a boolean value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bits can be examined, set, or cleared. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations.

Note : By default, all bits in the set initially have the value false. And something else to be concerned with is that when you pass a null parameter to any of the methods in a BitSet, that will result in a NullPointerException. A BitSet is not safe for multithreaded use without external synchronization.

BitSet Class Structure

public class BitSet extends Object implements Cloneable, Serializable

Constructor Details

This class contains two constructors; one is a default constructor for creating default objects of a BitSet class and another is a constructor that takes as an int type one argument whose initial size is large enough to explicitly represent the bits with indices in the range 0 through nbits-1. All bits are initially false.

public BitSet()

public BitSet(int nbits)

Methods Details :

 bitset1.jpg

 bitset2.jpg

Example :

import java.util.BitSet;

 

class MyBitSetDemo {

      public static void main(String args[]) {

            BitSet bits1 = new BitSet(12);

            BitSet bits2 = new BitSet(24);

            // set some bits

            for (int i = 10; i < 32; i++) {

                  if ((i % 2) == 0)

                        bits1.set(i);

                  if ((i % 5) != 0)

                        bits2.set(i);

            }

            System.out.println("Initial  bits1: ");

            System.out.println(bits1);

            System.out.println("\\nInitial bits2: ");

            System.out.println(bits2);

            // AND bits

            bits2.and(bits1);

            System.out.println("\\nbits2 AND bits1: ");

            System.out.println(bits2);

            // OR bits

            bits2.or(bits1);

            System.out.println("\\nbits2 OR bits1: ");

            System.out.println(bits2);

            // XOR bits

            bits2.xor(bits1);

            System.out.println("\\nbits2 XOR bits1: ");

            System.out.println(bits2);

      }

}

OUTPUT:

 bitset cmd.jpg

COMMENT USING

Trending up