Java.util.BitSet.clear() in Java
There are three variants of clear() method:
- clear() : The clear() method sets all of the bits in this BitSet to false.
public void clear()
Return Value
This method does not return a value.
import java.util.*;
public class BitClr1 {
public static void main(String[] args)
{
BitSet bset = new BitSet( 8 );
for ( int i = 0 ; i < 5 ; i++)
bset.set(i);
System.out.println
( "The bitset before clear() operation is : " + bset);
bset.clear();
System.out.println
( "The bitset after clear() operation is : " + bset);
}
}
|
Output:
The bitset before clear() operation is : {0, 1, 2, 3, 4}
The bitset after clear() operation is : {}
- clear(int pos) : The clear(int pos) method sets the bit specified by the index to false. i.e removes from bitset.
public void clear(int pos)
Parameters
pos : the index of the bit to be cleared.
Return Value
This method does not return a value.
Exception
IndexOutOfBoundsException : if the specified index is negative.
import java.util.*;
public class BitClr2 {
public static void main(String[] args)
{
BitSet bset = new BitSet( 8 );
for ( int i = 0 ; i < 5 ; i++)
bset.set(i);
System.out.println
( "The bitset before clear(pos) operation is : " + bset);
bset.clear( 3 );
System.out.println
( "The bitset after clear(pos) operation is : " + bset);
}
}
|
Output:
The bitset before clear(pos) operation is : {0, 1, 2, 3, 4}
The bitset after clear(pos) operation is : {0, 1, 2, 4}
- clear(int frompos, int topos) : The clear(int frompos, int topos) method sets the bits from the specified frompos (inclusive) to the specified topos (exclusive) to false i.e performs removal in a range.
public void clear(int frompos, int topos)
Parameters
frompos : index of the first bit to be cleared
topos : index of the last bit to be cleared
Return Value
This method does not return a value.
Exception
IndexOutOfBoundsException: if frompos is negative,
or topos is negative, or frompos is larger than topos.
import java.util.*;
public class BitClr3 {
public static void main(String[] args)
{
BitSet bset = new BitSet( 8 );
for ( int i = 0 ; i < 5 ; i++)
bset.set(i);
System.out.println
( "The bitset before clear(frompos, topos) operation is : " + bset);
bset.clear( 2 , 4 );
System.out.println
( "The bitset after clear(frompos, topos) operation is : " + bset);
}
}
|
Output:
The bitset before clear(frompos, topos) operation is : {0, 1, 2, 3, 4}
The bitset after clear(frompos, topos) operation is : {0, 1, 4}
Last Updated :
16 Aug, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...