Java BitSet flip() method7 Jan 2025 | 3 min read The flip() method of Java BitSet class sets the bit set to its complement. For example, if a bit value contains a true value then if you apply flip() operation on it, it will return false. There are two overloaded flip() method available in BitSet class. These methods are differentiated on the basis of its parameter. 1. Java BitSet flip(int bitIndex) methodThe flip(int bitIndex) method sets the bit to its complement at the specified index. 2. Java BitSet flip(int fromIndex, int toIndex) methodThe flip(int fromIndex, int toIndex) method set each bit value to its complement from specified inclusive fromIndex to exclusive toIndex. Syntax:Parameter:
Returns:NA Exception:
Compatibility Version:Java 1.4 and above Example of Java BitSet flip(int bitIndex) methodExample 1Output: bitset: {0, 1, 2, 3} bitset value: true true true true bitset after flip index 1: {0, 2, 3} bitset value after flip index 1: true false true true Example 2The flip(int bitIndex) method throw IndexOutOfBoundsException if we provide negative index value. Output: Exception in thread "main" java.lang.IndexOutOfBoundsException: bitIndex < 0: -1 at java.util.BitSet.flip(Unknown Source) at BitSetFlipExample2.main(BitSetFlipExample2.java:12) bitset: {0, 1, 2, 3} bitset value: true true true true Example of Java BitSet flip(int fromIndex, int toIndex) methodExample 3Output: bitset: {0, 1, 2, 3, 5} bitset value: true true true true true bitset after flip index 1,3: {0, 3, 5} bitset value after flip index 1,3: true false false true true Example 4The flip(int fromIndex, int toIndex) method throw IndexOutOfBoundsException if toIndex is greater than fromIndex. Output: Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex: 3 > toIndex: 1 at java.util.BitSet.checkRange(Unknown Source) at java.util.BitSet.flip(Unknown Source) at BitSetFlipExample4.main(BitSetFlipExample4.java:13) bitset: {0, 1, 2, 3, 5} bitset value: true true true true true Next TopicJava-bitset-xor-method |
The size() method of Java BitSet class returns the total number of bit space actually in use by this BitSet to represent bit values. The maximum element in the set is the size - 1st element. The default size of the bit set is 64-bit space....
2 min read
Java BitSet iousClearBit() method The iousClearBit(int fromIndex) method of Java BitSet class returns the index of the nearest bit which is set to false (clear) that occurs on or before the specified index. It returns -1 if the specified index is negative or no such clear...
1 min read
The or(BitSet set) method of Java BitSet class is used to perform a logical OR operation of this bit set with the specified set argument. The value of bit set is modified so that each bit in it has true if and only if the...
3 min read
The toString() method of Java BitSet class returns a string representation of this bit set. The indexes of set bit are returned from lower index to higher index. These set bit are separated by a comma and space ", " surrounded by curly braces. Syntax: public String...
1 min read
Java BitSet ClearBit() method The ClearBit(int fromIndex) method of Java BitSet class returns the index of first bit which is set to false that occurs on or after the specified index. Syntax: public int ClearBit(int fromIndex) Parameter: DataType Parameter Description int fromIndex It is an index of BitSet from which the checking of clear bit...
1 min read
The cardinality() method of Java BitSet class returns the number of bits set which are true in this BitSet. Syntax: public int cardinality() Parameter: NA Returns: The cardinality() method returns the number of bits set which are true in this BitSet. Exception: NA Compatibility Version: Java 1.4 and above Example 1 import java.util.BitSet; public class BitSetCardinalityExample1 { public static...
2 min read
The equals() method of Java BitSet class is used to compare the current BitSet object with specified object. The result of comparing BitSet bit with BitSet object returns true if and only if the specified object is not null and the set of BitSet object...
3 min read
Java BitSet SetBit() method The SetBit(int fromIndex) method of Java BitSet class returns the index of the first bit which is set to true that occurs on or after the specified index. If the BitSet does not contain any set bit then it returns -1. Syntax: public int...
2 min read
The valueOf() method of Java BitSet class returns a new bit set which contains all the set bit of given parameter. Syntax: public static BitSet valueOf(byte[] bytes) public static BitSet valueOf(long[] longs) public static BitSet valueOf(ByteBuffer bb) public static BitSet valueOf(LongBuffer lb) Parameter: DataType Parameter Description byte bytes[] It is a byte array represents a sequence...
2 min read
The get() method of Java BitSet class returns the bit value. There are two overloaded get() method available in BitSet class. 1. Java BitSet get(int bitIndex) method The get(int bitIndex) method returns the bit value of the specified index. It returns true if the index bitIndex is...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India