Home »
Java programming language
Java BitSet clear() Method with Example
BitSet Class clear() method: Here, we are going to learn about the clear() method of BitSet Class with its syntax and example.
Submitted by Preeti Jain, on January 21, 2020
BitSet Class clear() method
Syntax:
public void clear();
public void clear(int bit_in);
public void clear(int st_in, int en_in);
- clear() method is available in java.util package.
- clear() method is used to clear all the bits presented in this BitSet.
- clear(int bit_in) method is used to clear the bit presented in this BitSet.
- clear(int st_in, int en_in) method is used to clear the bits presented in a range from st_in(starting bit) to en_in(ending bit) of this BitSet.
- clear() method does not throw an exception at the time of clearing bits in this bit set.
- clear(int bit_in) method may throw an exception at the time of assigning index.
IndexOutOfBoundsException: This exception may throw when the given bit_in (bit index) is less than 0.
- clear(int st_in, int en_in) method may throw an exception at the time of assigning index.
IndexOutOfBoundsException: This exception may throw when st_in or en_in is less than 0 or st_in>en_in.
- These are non-static methods, it is accessible with the class object and if we try to access these methods with the class name then we will get an error.
Parameter(s):
- In the first case, clear(), it does not accept any parameter.
-
In the second case, clear(int bit_in)
- int bit_in – represents the bit to be cleared.
-
In the third case, clear(int st_in, int en_in)
- int st_in – represent the first bit to be unset.
- int en_in – represents the ending bit to be unset.
Return value:
In all the cases, the return type of the method is void, it returns nothing.
Example:
// Java program to demonstrate the example
// of void clear() method of BitSet.
import java.util.*;
public class ClearOfBitSet {
public static void main(String[] args) {
// create an object of BitSet
BitSet bs = new BitSet(10);
// By using set() method is to set
// the values in BitSet
bs.set(10);
bs.set(20);
bs.set(30);
bs.set(40);
bs.set(50);
bs.set(60);
bs.set(70);
bs.set(80);
// Display Bitset
System.out.println("bs: " + bs);
// By using clear(40) method is to remove
// the given bit
bs.clear(40);
// Display Bitset
System.out.println("bs.clear(40): " + bs);
// By using clear(20,50) method is to remove
// all the bits at the given range
bs.clear(20, 50);
// Display Bitset
System.out.println("bs.clear(20,50): " + bs);
// By using clear() method is to remove
// all the bits from BitSet
bs.clear();
// Display Bitset
System.out.println("bs.clear(): " + bs);
}
}
Output
bs: {10, 20, 30, 40, 50, 60, 70, 80}
bs.clear(40): {10, 20, 30, 50, 60, 70, 80}
bs.clear(20,50): {10, 50, 60, 70, 80}
bs.clear(): {}