Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Bitset Class

Java Bitset Class

Java Data StructuresJava Data Structures


The Bitset class creates a special type of array to hold bit values. The size of the array in BitSet grows as needed. This is similar to a bit vector (vector of bits).

This is a legacy class, but it was completely redesigned in Java 2.

BitSet defines two constructors.

The first constructor creates a default object:


BitSet()

The second method allows the user to specify the initial size. All bits are initialized to 0.


BitSet(int size)

The methods defined in the Cloneable interface implemented by BitSet are listed in the table below:

No. Method Description
1 void and(BitSet set)
Performs a logical AND of this target bit set with the parameter bit set.
2 void andNot(BitSet set)
Clears all bits in this BitSet whose corresponding bit is set in the specified BitSet.
3 int cardinality( )
Returns the number of bits set to true in this BitSet.
4 void clear( )
Sets all bits in this BitSet to false.
5 void clear(int index)
Sets the bit at the specified index to false.
6 void clear(int startIndex, int endIndex)
Sets the bits in the range from the specified startIndex (inclusive) to the specified toIndex (exclusive) to false.
7 Object clone( )
Clones this BitSet, producing a new BitSet equal to it.
8 boolean equals(Object bitSet)
Compares this object with the specified object.
9 void flip(int index)
Sets the bit at the specified index to the complement of its current value.
10 void flip(int startIndex, int endIndex)
Sets each bit in the range from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.
11 boolean get(int index)
Returns the bit value at the specified index.
12 BitSet get(int startIndex, int endIndex)
Returns a new BitSet composed of the bits from fromIndex (inclusive) to toIndex (exclusive) of this BitSet.
13 int hashCode( )
Returns the hash code value of this bit set.
14 boolean intersects(BitSet bitSet)
Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet.
15 boolean isEmpty( )
Returns true if this BitSet contains no bits set to true.
16 int length( )
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus 1.
17 int nextClearBit(int startIndex)
Returns the index of the first bit set to false, occurring at or after the specified starting index.
18 int nextSetBit(int startIndex)
Returns the index of the first bit set to true, occurring at or after the specified starting index.
19 void or(BitSet bitSet)
Performs a logical OR of this bit set with the bit set parameter.
20 void set(int index)
Sets the bit at the specified index to true.
21 void set(int index, boolean v)
 Sets the bit at the specified index to the specified value.
22 void set(int startIndex, int endIndex)
Sets the bits in the range from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true.
23 void set(int startIndex, int endIndex, boolean v)
Sets the bits in the range from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the specified value.
24 int size( )
Returns the number of bits actually used to represent the bit values of this BitSet.
25 String toString( )
Returns the string representation of this bit set.
26 void xor(BitSet bitSet)
Performs a logical XOR of this bit set with the bit set parameter.

Example

The following program illustrates several methods supported by this data structure:

Example

import

java

.

util

.

BitSet

;

public

class

BitSetDemo

{

public

static

void

main

(

String

args

[

]

)

{

BitSet

bits1

=

new

BitSet

(

16

)

;

BitSet

bits2

=

new

BitSet

(

16

)

;

//

set some bits

for

(

int

i

=

0

;

i

<

16

;

i

++

)

{

if

(

(

i

%

2

)

==

0

)

bits1

.

set

(

i

)

;

if

(

(

i

%

5

)

!=

0

)

bits2

.

set

(

i

)

;

}

System

.

out

.

println

(

"

Initial pattern in bits1:

"

)

;

System

.

out

.

println

(

bits1

)

;

System

.

out

.

println

(

"

\n

Initial pattern in bits2:

"

)

;

System

.

out

.

println

(

bits2

)

;

//

AND bits

bits2

.

and

(

bits1

)

;

System

.

out

.

println

(

"

\n

bits2 AND bits1:

"

)

;

System

.

out

.

println

(

bits2

)

;

//

OR bits

bits2

.

or

(

bits1

)

;

System

.

out

.

println

(

"

\n

bits2 OR bits1:

"

)

;

System

.

out

.

println

(

bits2

)

;

//

XOR bits

bits2

.

xor

(

bits1

)

;

System

.

out

.

println

(

"

\n

bits2 XOR bits1:

"

)

;

System

.

out

.

println

(

bits2

)

;

}

}

The compilation and execution results of the above example are as follows:


Initial pattern in bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

Initial pattern in bits2:
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}

bits2 AND bits1:
{2, 4, 6, 8, 12, 14}

bits2 OR bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

bits2 XOR bits1:
{}

Java Data StructuresJava Data Structures