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.

Flipping an Entry of BitSet Example

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);
   }
}

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, 3, 4, 5}

Flipping Multiple Entries of BitSet Example

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);
   }
}

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}

Flipping Multiple Entries of BitSet of Bytes Example

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);
   }
}

Output

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}
java_util_bitset.htm
Advertisements