Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
BitSet size() Method in Java with Examples
Next article icon

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

Last Updated : 16 Aug, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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



    Next Article
    BitSet size() Method in Java with Examples

    M

    Mohit Gupta
    Improve
    Article Tags :
    • Java
    • Java - util package
    • Java-BitSet
    • Java-Library
    Practice Tags :
    • Java

    Similar Reads

    • Java.util.BitSet class in Java with Examples | Set 1
      BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Constructors: BitSet class Constructors / \ BitSet() BitSet(int no_Of_Bits)BitSet() : A no-argument constructor to create an empty BitSet object. BitSet(int no_Of_Bits): A one-constructor w
      2 min read
    • BitSet class methods in Java with Examples | Set 3
      BitSet class methods in Set 3. / / | | | \ \ and notand flip isEmpty equal get intersect BitSet class methods in Java with Examples | Set 2 BitSet class methods are explained as follows : and / notand : java.util.BitSet.and() and java.util.BitSet.notand() method is a java.util.Bitset class method. .
      5 min read
    • BitSet size() Method in Java with Examples
      The size() Method of BitSet class in Java is used to know the size of this BitSet. This size is equal to the number of bits, each element has occupied in the BitSet. The maximum element in the set is the size - the first element Syntax: BitSet.hashCode() Parameters: The method does not accept any pa
      2 min read
    • BitSet clone() Method in Java with Examples
      The clone() Method Java.util.BitSet class is used to create a copy of an existing BitSet. The new BitSet is exactly equal to the existing one and is a mere copy of the previous BitSet. Syntax: Bit_Set.clone() Parameters: The method does not take any parameters. Return Value: The method just returns
      2 min read
    • BitSet toString() Method in Java with Examples
      The java.util.BitSet.toString() is an inbuilt method of BitSet class that is used to get a string representation of the bits of the sets in the form of a set of entries separated by “, “. So basically the toString() method is used to convert all the elements of BitSet into String. In addition to thi
      2 min read
    • BitSet equals() Method in Java with Examples
      The equals() method of Java BitSet class is used to check for equality between two bitsets. It verifies whether the elements of one set passed as a parameter is equal to the elements of this set or not. The method returns true if the bitsets match else false. Syntax: Bit_Set1.equals(Bit_Set2) Parame
      2 min read
    • BitSet stream() Method in Java with Examples
      The stream() method of Java BitSet class is used to return a stream of indices for every bit contained in the BitSet. The indices are returned in increasing order. The size of the stream is the number of bits in the set state of the BitSet, which is equal to the value returned by the cardinality() m
      2 min read
    • Java.util.BitSet.set() method in Java
      There are four variants of set() method.This article depicts about all of them, as follows: 1. set(int Index) : This method sets the bit at the specified index to true i.e adds a value. Declaration : public void set(int bitIndex) Parameters : Index : a bit index. Result : This method does not return
      3 min read
    • Set clear() method in Java with Examples
      The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me
      1 min read
    • Set contains() method in Java with Examples
      The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of Set. This is the eleme
      2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences