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

Published on Jul 03,2019 4K Views

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

edureka.co

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:

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

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: 

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:-

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-

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

 

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

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

 

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- 

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-

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 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework