set(int): This method is available in java.util.BitSet class of Java.
Syntax:
void java.util.BitSet.set(int bitIndex)
This method takes one argument of type int as its parameter. This method sets the bit at the specified index to true.
Parameters: One parameter is required for this method.
bitIndex: a bit index.
Throws:
IndexOutOfBoundsException - if the specified index is negative.
Approach 1: When no exception
Java
import java.util.BitSet;public class BitSetset {public static void main(String[] args) {BitSet bitSet = new BitSet(100);int bitIndex = 2;bitSet.set(bitIndex);System.out.println(bitSet);}}
Output:
{2}
Approach 2: IndexOutOfBoundsException
Java
import java.util.BitSet;public class BitSetset {public static void main(String[] args) {BitSet bitSet = new BitSet(100);int bitIndex = -1;bitSet.set(bitIndex);System.out.println(bitSet);}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: bitIndex < 0: -1 at java.base/java.util.BitSet.set(BitSet.java:447)
No comments:
Post a Comment