Java BitSet - flip() Method
The java.util.BitSet.flip() method is used to set the bit at the specified index to the complement of its current value in the given BitSet.
Syntax
public void flip(int bitIndex)
Parameters
bitIndex |
Specify the index of the bit to flip. |
Return Value
void type.
Exception
Throws IndexOutOfBoundsException, if the specified index is negative.
Example:
In the example below, the java.util.BitSet.flip() method is used to flip the bit at the specified index in the given BitSet.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a BitSet BitSet BSet = new BitSet(); //populating the BitSet BSet.set(10); BSet.set(20); BSet.set(30); BSet.set(40); BSet.set(50); //printing the BitSet System.out.println("BSet contains: " + BSet); //flipping the bit for index 30, 35 and 40 BSet.flip(30); BSet.flip(35); BSet.flip(40); //printing the BitSet System.out.println("BSet contains: " + BSet); } }
The output of the above code will be:
BSet contains: {10, 20, 30, 40, 50} BSet contains: {10, 20, 35, 50}
❮ Java.util - BitSet