
- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java BitSet clear() Method
Description
The Java BitSet clear() method sets all of the bits in this BitSet to false.
Declaration
Following is the declaration for java.util.BitSet.clear() method
public void clear()
Parameters
NA
Return Value
This method does not return a value.
Exception
NA
Java.util.BitSet.clear(int index) Method
Description
The java.util.BitSet.clear(int bitIndex) method sets the bit specified by the index to false.
Declaration
Following is the declaration for java.util.BitSet.clear() method
public void clear(int bitIndex)
Parameters
bitIndex − 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.
Java.util.BitSet.clear(int fromIndex,int toIndex) Method
Description
The java.util.BitSet.clear(int fromIndex,int toIndex) method sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to false.
Declaration
Following is the declaration for java.util.BitSet.clear() method
public void clear(int fromIndex,int toIndex)
Parameters
fromIndex − index of the first bit to be cleared
toIndex − index of the last bit to be cleared
Return Value
This method does not return a value.
Exception
IndexOutOfBoundsException − if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.
Example 1
The following example shows the usage of Java BitSet clear() method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using clear() method we're resetting all bits to be false and printing the bitset cardinality.
package com.tutorialspoint; import java.util.BitSet; public class BitSetDemo { public static void main(String[] args) { // create a bitset BitSet bitset = new BitSet(); // assign values to bitset bitset.set(0, 6, true); // print the set System.out.println("Bitset:" + bitset); // clear the bitset bitset.clear(); // print cardinality for bitset, it should be zero System.out.println(bitset.cardinality()); } }
Let us compile and run the above program, this will produce the following result −
Bitset:{0, 1, 2, 3, 4, 5} 0
Example 2
The following example shows the usage of Java BitSet clear(index) method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using clear(index) method we're resetting one of the bits to be false and printing the bitset.
package com.tutorialspoint; import java.util.BitSet; public class BitSetDemo { public static void main(String[] args) { // create a bitset BitSet bitset = new BitSet(); // assign values to bitset bitset.set(0, 6, true); // print the set System.out.println("Bitset:" + bitset); // clear the bitset value at index 3 bitset.clear(3); // print the bitset System.out.println("Bitset:" + bitset); } }
Let us compile and run the above program, this will produce the following result −
Bitset:{0, 1, 2, 3, 4, 5} Bitset:{0, 1, 2, 4, 5}
Example 3
The following example shows the usage of Java BitSet clear(fromIndex, toIndex) method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using clear(fromIndex, toIndex) method we're resetting few of the bits to be false and printing the bitset.
package com.tutorialspoint; import java.util.BitSet; public class BitSetDemo { public static void main(String[] args) { // create a bitset BitSet bitset = new BitSet(); // assign values to bitset bitset.set(0, 6, true); // print the set System.out.println("Bitset:" + bitset); // clear the bitset value from index 3(inclusive) to 5(exclusive) bitset.clear(3, 5); // print the bitset System.out.println("Bitset:" + bitset); } }
Let us compile and run the above program, this will produce the following result −
Bitset:{0, 1, 2, 3, 4, 5} Bitset:{0, 1, 2, 5}