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
- 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
- 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
- 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.
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();
bs1.set( 0 );
bs1.set( 1 );
bs1.set( 2 );
bs1.set( 4 );
bs2.set( 4 );
bs2.set( 6 );
bs2.set( 5 );
bs3 = (BitSet) bs1.clone();
System.out.println( "bs1 : " + bs1);
System.out.println( "bs2 : " + bs2);
System.out.println( "bs3 cloned from bs1 : " + bs3);
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
- 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
- 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
- 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
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet();
bs1.set( 7 );
bs1.set( 1 );
bs1.set( 2 );
bs1.set( 4 );
bs1.set( 3 );
bs1.set( 6 );
bs2.set( 4 );
bs2.set( 6 );
bs2.set( 3 );
bs2.set( 9 );
bs2.set( 2 );
System.out.println( "bs1 : " + bs1);
System.out.println( "bs2 : " + bs2);
System.out.println( "use of length() : " + bs1.length());
bs1.xor(bs2);
System.out.println( "Use of xor() : " + bs1);
bs2.xor(bs1);
System.out.println( "Use of xor() : " + bs2);
bs2.clear( 1 , 2 );
System.out.println( "bs2 after clear method : " + bs2);
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
Last Updated :
16 Aug, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...