public final class BitArray
extends java.lang.Object
implements java.lang.Cloneable
A simple, fast array of bits, represented compactly by an array of ints internally.
Modifier and Type | Field and Description |
---|---|
private int[] |
bits |
private static int[] |
EMPTY_BITS |
private static float |
LOAD_FACTOR |
private int |
size |
Constructor and Description |
---|
BitArray() |
BitArray(int size) |
BitArray(int[] bits,
int size) |
Modifier and Type | Method and Description |
---|---|
void |
appendBit(boolean bit) |
void |
appendBitArray(BitArray other) |
void |
appendBits(int value,
int numBits)
Appends the least-significant bits, from value, in order from most-significant to
least-significant.
|
void |
clear()
Clears all bits (sets to false).
|
BitArray |
clone() |
private void |
ensureCapacity(int newSize) |
boolean |
equals(java.lang.Object o) |
void |
flip(int i)
Flips bit i.
|
boolean |
get(int i) |
int[] |
getBitArray() |
int |
getNextSet(int from) |
int |
getNextUnset(int from) |
int |
getSize() |
int |
getSizeInBytes() |
int |
hashCode() |
boolean |
isRange(int start,
int end,
boolean value)
Efficient method to check if a range of bits is set, or not set.
|
private static int[] |
makeArray(int size) |
void |
reverse()
Reverses all bits in the array.
|
void |
set(int i)
Sets bit i.
|
void |
setBulk(int i,
int newBits)
Sets a block of 32 bits, starting at bit i.
|
void |
setRange(int start,
int end)
Sets a range of bits.
|
void |
toBytes(int bitOffset,
byte[] array,
int offset,
int numBytes) |
java.lang.String |
toString() |
void |
xor(BitArray other) |
private static final int[] EMPTY_BITS
private static final float LOAD_FACTOR
private int[] bits
private int size
public BitArray()
public BitArray(int size)
BitArray(int[] bits, int size)
public int getSize()
public int getSizeInBytes()
private void ensureCapacity(int newSize)
public boolean get(int i)
i
- bit to getpublic void set(int i)
i
- bit to setpublic void flip(int i)
i
- bit to setpublic int getNextSet(int from)
from
- first bit to checkgetNextUnset(int)
public int getNextUnset(int from)
from
- index to start looking for unset bitsize
if none are unset until the endgetNextSet(int)
public void setBulk(int i, int newBits)
i
- first bit to setnewBits
- the new value of the next 32 bits. Note again that the least-significant bit
corresponds to bit i, the next-least-significant to i+1, and so on.public void setRange(int start, int end)
start
- start of range, inclusive.end
- end of range, exclusivepublic void clear()
public boolean isRange(int start, int end, boolean value)
start
- start of range, inclusive.end
- end of range, exclusivevalue
- if true, checks that bits in range are set, otherwise checks that they are not setjava.lang.IllegalArgumentException
- if end is less than start or the range is not contained in the arraypublic void appendBit(boolean bit)
public void appendBits(int value, int numBits)
value
- int
containing bits to appendnumBits
- bits from value to appendpublic void appendBitArray(BitArray other)
public void xor(BitArray other)
public void toBytes(int bitOffset, byte[] array, int offset, int numBytes)
bitOffset
- first bit to start writingarray
- array to write into. Bytes are written most-significant byte first. This is the opposite
of the internal representation, which is exposed by getBitArray()
offset
- position in array to start writingnumBytes
- how many bytes to writepublic int[] getBitArray()
public void reverse()
private static int[] makeArray(int size)
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public BitArray clone()
clone
in class java.lang.Object