Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaJava.util.BitSet class methods in Java with Examples | Set 2

Java.util.BitSet class methods in Java with Examples | Set 2

Methods discussed in this post:
                       BitSet class methods.
               /      /     |      |       \       \
           set()  xor()  clone() clear()  length()  cardinality()

We strongly recommend to refer below set 1 as a prerequisite of this.
BitSet class in Java | Set 1

  1. set() : java.util.BitSet.set() method is a sets the bit at the specified index to the specified value.
    Syntax:
    public void set(int bitpos)
    public void set(int bitpos, boolean val)
    Parameters:
    bitpos : a bit index
    val : a boolean value to set
    Return: Nothing
    Throws: IndexOutOfBoundsException - if the specified index is negative
    
  2. clone() : java.util.BitSet.clone() method clones a BitSet produces a new BitSet that is equal to it. The clone of the bit set is another bit set that has exactly the same bits set to true as this bit set.
    Syntax:
    public Object clone()
    Return: a clone of this bit set
    
  3. cardinality : java.util.BitSet.cardinality() method is used to find the no. of elements in Bitset.
    Syntax:
    public int cardinality()
    Return: the number of bits set to true in this BitSet.
    



  4. // Java program explaining BitSet class methods
    // set(), clone(), cardinality()
    import java.util.*;
    public class NewClasss
    {
        public static void main(String[] args)
        {
            BitSet bs1 = new BitSet();
            BitSet bs2 = new BitSet(8);
            BitSet bs3 = new BitSet();
      
            // assign values to bs1 using set()
            bs1.set(0);
            bs1.set(1);
            bs1.set(2);
            bs1.set(4);
      
            // assign values to bs2
            bs2.set(4);
            bs2.set(6);
            bs2.set(5);
      
            // Here we are using .clone() method to make
            // bs3 as bs1
            bs3 = (BitSet) bs1.clone();
      
            // Printing the 3 Bitsets
            System.out.println("bs1 : " + bs1);
            System.out.println("bs2 : " + bs2);
            System.out.println("bs3 cloned from bs1 : " + bs3);
      
            // Using .cardinality() method to print the no. of
            // elements of Bitset
            System.out.println("Cardinality of bs1 : " +
                                           bs1.cardinality());
            System.out.println("Cardinality of bs2 : " +
                                           bs2.cardinality());
        }
    }

    
    

    Output:

    bs1 : {0, 1, 2, 4}
    bs2 : {4, 5, 6}
    bs3 cloned from bs1 : {0, 1, 2, 4}
    Cardinality of bs1 : 4
    Cardinality of bs2 : 3
    
  5. clear() : java.util.BitSet.clear() method is used to clear the elements i.e. set all the Bitset elements to false.
    Syntax:
    public void clear(int frompos,int topos)
    public void clear(int bitIndex)
    public void clear()
    Parameters:
    frompos - index of the first bit to be cleared
    topos - index after the last bit to be cleared
    bitIndex - the index of the bit to be cleared
    Throws:
    IndexOutOfBoundsException - if pos value is negative or frompos is larger than topos
    
  6. xor() : java.util.BitSet.xor() method performs the logical Xor operation on the bitsets.
    This bit set is modified so that a bit in it has the value true if and only if :
    • The bit initially has the value true, and the corresponding bit in the argument has the value false.
    • The bit initially has the value false, and the corresponding bit in the argument has the value true.

    Syntax:

    public void xor(BitSet set)
    Parameters:
    set - the BitSet with which we need to perform operation
    
  7. length() : java.util.BitSet.length() method return returns logical size of the bitset.
    The index of the highest set bit in the bitset plus one. Returns zero if the bitset contains no set bits.
    Syntax:
    public int length()
    Returns:
    the logical size of this BitSet
    



  8. // Java program explaining BitSet class methods
    //  xor(), length(), clear() methods
    import java.util.*;
    public class NewClass
    {
        public static void main(String[] args)
        {
            BitSet bs1 = new BitSet();
            BitSet bs2 = new BitSet();
      
            // assign values to bs1 using set()
            bs1.set(7);
            bs1.set(1);
            bs1.set(2);
            bs1.set(4);
            bs1.set(3);
            bs1.set(6);
      
            // assign values to bs2
            bs2.set(4);
            bs2.set(6);
            bs2.set(3);
            bs2.set(9);
            bs2.set(2);
      
            // Printing the Bitsets
            System.out.println("bs1 : " + bs1);
            System.out.println("bs2 : " + bs2);
      
            // use of length() method
            System.out.println("use of length() : " + bs1.length());
      
            // use of xor() to perform logical Xor operation
            bs1.xor(bs2);
            System.out.println("Use of xor() : " + bs1);
            bs2.xor(bs1);
            System.out.println("Use of xor() : " + bs2);
      
            // clear from index 2 to index 4 in bs1
            bs2.clear(1, 2);
            System.out.println("bs2 after clear method : " + bs2);
      
            // clear complete Bitset
            bs1.clear();
            System.out.println("bs1 after clear method : " + bs1);
        }
    }

    
    

    Output:

    bs1 : {1, 2, 3, 4, 6, 7}
    bs2 : {2, 3, 4, 6, 9}
    use of length() : 8
    Use of xor() : {1, 7, 9}
    Use of xor() : {1, 2, 3, 4, 6, 7}
    bs2 after clear method : {2, 3, 4, 6, 7}
    bs1 after clear method : {}
    


    BitSet class methods in Java with Examples | Set 3

    References :
    https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html

    This article is contributed by Mohit Gupta. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

RELATED ARTICLES

Most Popular

Recent Comments