The Java BitSet clear() method sets all of the bits in this BitSet to false.
DeclarationFollowing is the declaration for java.util.BitSet.clear() method
public void clear()Parameters
NA
Return ValueThis method does not return a value.
ExceptionNA
Java BitSet clear(int index) Method DescriptionThe Java BitSet clear(int bitIndex) method sets the bit specified by the index to false.
DeclarationFollowing 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 ValueThis method does not return a value.
ExceptionIndexOutOfBoundsException − if the specified index is negative.
Java BitSet clear(int fromIndex,int toIndex) Method DescriptionThe Java BitSet clear(int fromIndex,int toIndex) method sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to false.
DeclarationFollowing 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
This method does not return a value.
ExceptionIndexOutOfBoundsException − if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.
Clearing a BitSet ExampleThe 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()); } }Output
Let us compile and run the above program, this will produce the following result −
Bitset:{0, 1, 2, 3, 4, 5} 0Clearing an element of BitSet Example
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); } }Output
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}Clearing a subset of BitSet Example
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); } }Output
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}
java_util_bitset.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4