BitSet

About

The BitSet class in Java is a part of the java.util package and provides an efficient way to work with a sequence of bits. It represents a vector of bits that grows dynamically as needed. Each bit in the BitSet can either be true (set) or false (unset), making it an ideal choice for tasks such as manipulating sets of flags, managing collections of Boolean values, or performing operations on binary data.

Internally, BitSet uses a long array to store bits, which allows it to be memory efficient, especially compared to using an array of boolean primitives.

How BitSet Stores Bits Internally

  • A BitSet uses an array of long values (long[]) to store the bits.

  • Each long in Java is 64 bits wide, meaning one long can store the state of 64 bits (true or false values).

  • If you need to store, say, 128 bits, a BitSet will use 2 longs (128 / 64 = 2).

How boolean[] Stores Values Internally

  • In a boolean[], each element represents one bit of information (true or false), but due to memory alignment and storage constraints, Java does not store booleans as single bits.

  • Instead, Java typically uses 1 byte (8 bits) to store each boolean value in an array, because most hardware cannot directly address individual bits.

  • So, a boolean[] of size 128 will use 128 bytes (1 byte per boolean).

Features

  1. Dynamic Sizing: The size of a BitSet is not fixed and can grow automatically as bits are set or cleared.

  2. Memory Efficiency: BitSet is much more memory efficient than a boolean[] array since it stores bits compactly using long integers.

  3. Bitwise Operations: It supports logical operations such as AND, OR, XOR, and AND NOT.

  4. Index-Based Access: Bits can be accessed and manipulated using their zero-based index.

  5. Stream and Lambda Support: Java 8 and later versions added support for streaming through BitSet using methods like stream().

  6. Automatic Growth: When setting a bit at an index that exceeds the current size, the BitSet automatically resizes to accommodate the new index.

  7. Default State: All bits in a BitSet are initially false.

  8. Serialization: The BitSet class implements Serializable, allowing it to be serialized and deserialized.

Declaration & Functions

Declaration

To use a BitSet, simply import it and create an instance:

Key Methods and Functions

  1. Setting and Clearing Bits:

  2. Getting Bit Values:

  3. Checking State:

  4. Logical Operations:

  5. Stream Support (Java 8+):

  6. Miscellaneous:

Usage

1. Flags and Binary Representations:

  • BitSet is often used to represent flags or enable/disable states in an application.

2. Efficient Membership Testing:

  • It can be used to represent sets and perform operations like intersection and union.

3. Data Compression:

  • To store large numbers of Boolean values in minimal memory.

4. Prime Number Generation (Sieve of Eratosthenes):

  • BitSet is used to efficiently implement algorithms for generating prime numbers.

5. Stream-Based Processing:

  • Using BitSet in conjunction with streams for advanced data processing.

Last updated