Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Bitset In Java: All You Need To know About Bitset Methods In Java

Published on Jul 03,2019 3.9K Views


Java Certification is one of the most looked after certification for the programmers. The major reason for that is Java offers great amount flexibility and different features to simplify various tasks. This article introduces you to one such feature that is ‘Bitsets In Java‘. These are pointers this article will focus on:

So let us start with the first topic of discussion then,

What are Bitsets in Java?

Bitsets represents fixed size sequence of  N bits having values either zero or one. Zero means value is false or unset. One means value is true or set. Bitset size is fixed at compile time. Bitset is a class defined in java.util package. It is a special type of array which holds bit values. It implements a vector of bits. Its size grows automatically as more bits are needed.

This class provides us two types of constructors to form bitset from integers as well as from strings. Those two are:

  • Bitset( ): It is a no-argument constructor to create a default object.
  • Bitset(int size): It is a one-constructor having integer arguments to form an instance of the bitset class with an initial size of the integer argument representing the no. of bits.

For example:


 import java.util.BitSet;
public class BitSetJavaExample
{
public static void main(String args[])
{
int n=8;
BitSet p = new BitSet(n);
for(int i=0;i<n;i++)
p.set(i);
System.out.print("Bits of p are set as : ");
for(int i=0;i<n;i++)
System.out.print(p.get(i)+" ");
BitSet q = (BitSet) p.clone();
System.out.print("nBits of q are set as : ");
for(int i=0;i<n;i++)
System.out.print(q.get(i)+" ");
for(int i=0;i<3;i++)
p.clear(i);
System.out.print("nBits of p are now set as : ");
for(int i=0;i<n;i++)
System.out.print(p.get(i)+" ");
System.out.print("nBits of p which are true : "+p);
System.out.print("The Bits of q which are true : "+q);
BitSet r= (BitSet) p.clone();
p.xor(q);
System.out.println("Output of p xor q= "+p);
p = (BitSet) r.clone();
p.and(q);
System.out.println("Output of p and q = "+p);
p = (BitSet) r.clone();
p.or(q);
System.out.println("Output of p or q = "+p);
}
}

OUTPUT

Output - Bitsets in Java - Edureka

Now let us move further and take a look the next topic of discussion in this article on Bits in Java,

BITSET JAVA METHODS AND DESCRIPTIONS

Bitset and ( ) method 

This method is used to perform logical AND operation of the target bitset with the specified argument. The value of set comes true only and only if the both bitset initially and the corresponding bit set has the true value.

Syntax: public void and(BitSet set)

Example:


import java.util.BitSet;
public class BitSetAndExample2
{
public static void main(String[] args)
{
// create 2 bitsets
BitSet bitset1 = new BitSet();
BitSet bitset2 = new BitSet();

// assign values to bitset1
bitset1.set(1);
bitset1.set(2);
bitset1.set(3);
bitset1.set(6);
bitset1.set(7);

// assign values to bitset2
bitset2.set(10);
bitset2.set(20);
bitset2.set(30);
bitset2.set(40);
bitset2.set(60);

// print the sets
System.out.println("bitset1: " + bitset1);
System.out.println("bitset2: " + bitset2);

// perform and operation between two bitsets
bitset1.and(bitset2);
// print the new bitset1
System.out.println("result bitset: " + bitset1);
}
}

Output: 

Output - Bitsets in Java - Edureka

Bitset andNot ( ) Method

This method is used to clear the entire bit in the bitset whose corresponding bits are already set in the specified bitset.

Syntax-  public void andNot (BitSet set)

Example:


import java.util.BitSet;
public class BitSetAndNotExample2
{
public static void main(String[] args)
{
BitSet bitset1 = new BitSet();

bitset1.set(60);
bitset1.set(61);
bitset1.set(62);
bitset1.set(63);
bitset1.set(64);
// print the sets
System.out.println("bitset1: " + bitset1);

// perform andNot operation between bitset and null throw exception
bitset1.andNot(null);
// print the new bitset1
System.out.println("result bitset after andNot: " + bitset1);
}
}

Output:-

output - Edureka

Bitset cardinality( ) Method  

This method is  used to return the no.of bits only which are true in the bitset.

Syntax- public int cardinality( )

Example


import java.util.BitSet;
public class BitSetCardinalityExample1
{
public static void main(String[] args)
{
// create a bitset
BitSet bitset = new BitSet();

// assign values to bitset
bitset.set(10);
bitset.set(11);
bitset.set(12);
bitset.set(15);
bitset.set(16);

// print the sets
System.out.println("bitset: " + bitset);
int trueBits = bitset.cardinality();
// print bitset cardinality
System.out.println("number of true bits: " + trueBits);

bitset.clear(2);
System.out.println("bitset after clear index 2: " + bitset);
trueBits = bitset.cardinality();
// print bitset cardinality after clear index 2
System.out.println("number of true bits after clear index 2: " + trueBits);

}
}

Output-

output - Bitset In Java - Edureka

BitSet clone( ) Method

This method is used to make the clone of the bitset to new bitset. This bitset has equal to the current original bitset. The clone bitset carries exactly the same true values which the original bitset carries.

Syntax- public Object clone( )

Example– 


import java.util.BitSet;
public class BitSetCloneExample1
{
public static void main(String[] args)
{
BitSet bitsetOriginal = new BitSet(15);
bitsetOriginal.set(12);
bitsetOriginal.set(13);
bitsetOriginal.set(15);
bitsetOriginal.set(16);
bitsetOriginal.set(18);
//print current bitset
System.out.println("Current bitset: "+bitsetOriginal);
// making clone of current bitset
Object bitsetClone = bitsetOriginal.clone();
//print clone bitset
System.out.println("Clone bitset: "+bitsetClone);
}
}

Output

output - Bitset In Java - Edureka 

BitSet equals( ) Method 

This method of the JAVA bitset is used to compare the current bitset object with the specified bitset object.

The result of comparing bitset returns true if and only if the specified bitset object is not null and the set of   bitset object should have exactly the same set of bitset to true value as this Bitset.

Syntax- public boolean equals(object obj)

Example

</pre>
import java.util.BitSet;

public class BitSetEqualsExample1
{
public static void main(String[] args)
{
// creating bitset
BitSet bitset = new BitSet(15);

Object obj = new BitSet(15);

bitset.set(10);
bitset.set(11);
bitset.set(12);
bitset.set(13);
bitset.set(14);

((BitSet) obj).set(10);
((BitSet) obj).set(11);
((BitSet) obj).set(12);
((BitSet) obj).set(13);
((BitSet) obj).set(14);

// print current bitsets
System.out.println("bitset:" + bitset);
System.out.println("object:" + obj);
boolean bol = bitset.equals(obj);
if(bol==true) {
System.out.println("BitSet is equal to specified Object");
}
else {
System.out.println("BitSet is not equal to specified Object");
}
}
}
<pre>

Output

output - Bitset In Java - Edureka

BitSet isEmpty Method 

This method returns true if this bitset does not contain bits which are set to true. 

Syntax- public Boolean isEmpty( )

Example


import java.util.BitSet;

public class BitSetIsEmptyExample1
{
public static void main(String[] args)
{
BitSet bitset1 = new BitSet(15);
BitSet bitset2 = new BitSet(15);

bitset1.set(11);
bitset1.set(12);
bitset1.set(13);
bitset1.set(14);

System.out.println("bitset1: "+bitset1);
System.out.println("bitset2: "+bitset2);

//returns false as bitset1 is not empty
boolean b1 = bitset1.isEmpty();
//returns true as bitset2 is empty
boolean b2 = bitset2.isEmpty();

System.out.println("bitset1 isEmpty: "+b1);
System.out.println("bitset2 isEmpty: "+b2);
}
}

Output

output - Bitset In Java - Edureka 

BitSet length( ) Method 

This method returns the logical size of this bitset. The length goes up to the index of the highest set bit plus one. It returns zero if bit set does not contain any bit.

Syntax-public int length( )

Example-


import java.util.BitSet;

public class BitSetLengthExample1
{
public static void main(String[] args)
{
BitSet bitset1 = new BitSet(15);
BitSet bitset2 = new BitSet(15);
BitSet bitset3 = new BitSet(15);


bitset2.set(11);
bitset2.set(12);
bitset2.set(13);
bitset2.set(14);

bitset3.set(12);
bitset3.set(14);
bitset3.set(16);
bitset3.set(18);
bitset3.set(0);
bitset3.set(2);

System.out.println("bitset1: "+bitset1);
System.out.println("bitset2: "+bitset2);
System.out.println("bitset3: "+bitset3);

int length1 = bitset1.length();
int length2 = bitset2.length();
int length3 = bitset3.length();
System.out.println("length of bitset1: "+length1);
System.out.println("length of bitset2: "+length2);
System.out.println("length of bitset3: "+length3);
}
}

Output- 

output - Bitset In Java - Edureka

BitSet intersects( ) Method

This method returns the Boolean value true or false on the basis of whether the parameter bitset has intersected with the bitset or not.It returns true if the BitSet  set is also true in this bitset.

Syntax- public Boolean intersects( BitSet set  )

Example


import java.util.BitSet;

public class BitSetEntersectsExample2
{
public static void main(String[] args)
{
BitSet bitset = new BitSet(15);

bitset.set(11);
bitset.set(12);
bitset.set(13);
bitset.set(14);

System.out.println("bitset: "+bitset);
// perform andNot operation between bitset and null throw exception
boolean b = bitset.intersects(null);
System.out.println("intersected result between bitset and null: "+b);
}
}

Output-

output - Bitset In Java - Edureka

With the help of bitwise operators, we can implement various operations like AND, OR, NOT, XOR etc. They work on a smaller scale. They can be applied to any integer type. Bitwise operators operate on bit level. They are fast and require less memory. Many cryptography algorithms works at the bit level too.

This is it guys. This brings us to the end of this article on Bits In Java. I hope you enjoyed this piece of information. Check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 12th October,2024

12th October

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 14th December,2024

14th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Bitset In Java: All You Need To know About Bitset Methods In Java

edureka.co