
- 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 flip(int bitIndex) Method
Description
The Java BitSet flip(int bitIndex) method sets the bit at the specified index to the complement of its current value.
Declaration
Following is the declaration for java.util.BitSet.flip() method
public void flip(int bitIndex)
Parameters
bitIndex − the index of the bit to flip.
Return Value
This method does not return a value.
Exception
IndexOutOfBoundsException − if the specified index is negative.
Java BitSet flip(int fromIndex,int toIndex) Method
Description
The Java BitSet flip(int fromIndex,int toIndex) method sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.
Declaration
Following is the declaration for java.util.BitSet.flip() method
public void flip(int fromIndex,int toIndex)
Parameters
fromIndex − index of the first bit to flip.
toIndex − index after the last bit to flip.
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 flip(bitIndex) method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using flip(bitIndex) method we're flipping one bit to be false and printing the bitset again.
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); // change the bitset value at index 2 bitset.flip(2); // print the bitset again 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, 3, 4, 5}
Example 2
The following example shows the usage of Java BitSet flip(fromIndex, toIndex) method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using flip(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 at from index 3 to 5 bitset.flip(3,6); // 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}
Example 3
The following example shows the usage of Java BitSet flip(fromIndex, toIndex) method. We're creating two BitSets using byte[] 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 = BitSet.valueOf(new byte[] { 0, 1, 2, 3, 4, 5 }); // print the set System.out.println("Bitset:" + bitset); // clear the bitset value from index 30(inclusive) to 50(exclusive) bitset.clear(30, 50); // print the bitset System.out.println("Bitset:" + bitset); } }
Let us compile and run the above program, this will produce the following result −
Bitset:{8, 17, 24, 25, 34, 40, 42} Bitset:{8, 17, 24, 25}