Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Bitset

The BitSet class creates a special type of array that holds bit values. The BitSet array can increase in size as needed. This makes it similar to a vector of bits. This is a legacy class but it has been completely re-engineered in Java 2, version 1.4.

BitSet class Constructors

  • BitSet()
  • BitSet(int no_Of_Bits)

Important Points :

  • The size of the array is flexible and can grow to accommodate additional bit as needed.
  • As it is an array, the index is zero-based and the bit values can be accessed only by non-negative integers as an index .
  • The default value of the BitSet is boolean false with a representation as 0 (off).
  • Calling the clear method makes the bit values set to false.
  • BitSet uses about 1 bit per boolean value.
  • To access a specific value in the BitSet, the get method is used with an integer argument as an index.

Syntax:

To create a new bitset

 // Constructors of BitSet class
 BitSet bs1 = new BitSet();
 /* set is BitSet class method expalined in below part */
 // assigning values to bitset 1
 bs1.set(0); 
 bs1.set(1);

 

Important Methods in BitSet Class:

  • set() : java.util.BitSet.set() method is a sets the bit at the specified index to the specified value
  • 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.
  • cardinality() : java.util.BitSet.cardinality() method is used to find the no. of elements in Bitset
  • 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.

To read more about BitSets Click here

Try the following example in the editor below.

You are given two bitset b1 and b2, perform the operations defined in the comments in the editor below.

Start solving Bitset on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test