Sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.
Program
package com.candidjava;
import java.util.BitSet;
/**
* @author :AnandBabu M
* @description :flip(int fromIndex, int toIndex) Sets each bit from the
* specified fromIndex (inclusive) to the specified toIndex
* (exclusive) to the complement of its current value.
* */
public final class BitSetvoidflip {
public static void main(String[] args) {
BitSet obj = new BitSet(8);
BitSet obj1 = new BitSet(8);
obj.set(0);
obj.set(1);
obj.set(2);
obj.set(3);
obj1.set(2);
obj1.set(4);
obj1.set(6);
obj1.set(8);
System.out.println("The Value in obj:" + obj);
System.out.println("The Value in obj1" + obj1);
obj.flip(1, 3);
System.out.println("Now the value is" + obj);
obj1.flip(2, 6);
System.out.println("Now the value is" + obj1);
}
}
Output
The Value in obj:{0, 1, 2, 3}
The Value in obj1{2, 4, 6, 8}
Now the value is{0, 3}
Now the value is{3, 5, 6, 8}
Explanation
public void flip(int fromIndex,int toIndex)
Sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.
Parameters:
fromIndex - index of the first bit to flip
toIndex - index after the last bit to flip
Throws:
IndexOutOfBoundsException - if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex