Last Updated: 06 March, 2023
BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values.
The BitSet is a built-in class in java used to create a dynamic array of bits represented by boolean values. The BitSet class is available inside the java.util package. The BitSet array can increase in size as needed. This feature makes the BitSet similar to a Vector of bits. The bit values can be accessed by non-negative integers as an index. The size of the array is flexible and can grow to accommodate additional bit as needed. The default value of the BitSet is boolean false with a representation as 0 (off). BitSet uses 1 bit of memory per each boolean value.Java BitSet class declaration
S.No. | Constructor with Description |
---|---|
1. | public BitSet( ) It creates a default BitSet object. |
2. | public BitSet(int noOfBits) This method creates a BitSet object with the number of bits that it can hold. All bits are initialized to zero. It will throw a NegativeArraySizeException if the specified initial size is negative. |
S.No. | Methods with Description |
---|---|
1. | void and(BitSet bitSet) It performs an AND operation on the contents of the invoking BitSet object with those specified by bitSet. |
2. | void andNot(BitSet bitSet) For each 1 bit in bitSet, the corresponding bit in the invoking BitSet is cleared. |
3. | void flip(int index) It reverses the bit at the specified index. |
4. | void flip(int startIndex, int endIndex) It reverses all the bits from the specified startIndex to endIndex. |
5. | void or(BitSet bitSet) It performs an OR operation on the contents of the invoking BitSet object with those specified by bitSet. |
6. | void xor(BitSet bitSet) It performs XOR operation on the contents of the invoking BitSet object with those specified by bitSet. |
7. | int cardinality( ) It returns the number of bits set to true in the invoking BitSet. |
8. | void clear( ) It sets all the bits of the invoking BitSet to zeros. |
9. | void clear(int index) It set the bit specified by the given index to zero. |
10. | void clear(int startIndex, int endIndex) It sets all the bits from specified startIndex to endIndex to zero. |
11. | Object clone( ) It duplicates the invoking BitSet object. |
12. | boolean equals(Object bitSet) It returns true if both the invoking and argumented BitSets are equal; otherwise, it returns false. |
13. | boolean get(int index) It retruns the present state of the bit at given index in the invoking BitSet. |
14. | BitSet get(int startIndex, int endIndex) It returns a BitSet object that consists of all the bits from startIndex to endIndex. |
15. | int hashCode( ) It returns the hash code of the invoking BitSet. |
16. | boolean intersects(BitSet bitSet) It returns true if at least one pair of corresponding bits within the invoking object and bitSet are 1. |
17. | boolean isEmpty( ) It returns true if all bits in the invoking object are zero; otherwise, it returns false. |
18. | int length( ) It returns the total number of bits in the invoking BitSet. |
19. | int nextClearBit(int startIndex) It returns the index of the next cleared bit, (that is, the next zero bit), starting from the index specified by startIndex. |
20. | int nextSetBit(int startIndex) It returns the index of the next set bit (that is, the next 1 bit), starting from the index specified by startIndex. If no bit is set, -1 is returned. |
21. | void set(int index) It sets the bit specified by index. |
22. | void set(int index, boolean value) It sets the bit specified by index to the value passed in. |
23. | void set(int startIndex, int endIndex) It sets all the bits from startIndex to endIndex. |
24. | void set(int startIndex, int endIndex, boolean value) It sets all the bits to the specified value from startIndex to endIndex. |
25. | int size( ) It returns the total number of bits in the invoking BitSet. |
26. | String toString( ) It returns the string equivalent of the invoking BitSet object. |
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com