java.lang.Object
com.google.zxing.common.BitArray
- All Implemented Interfaces:
Cloneable
Field Summary
Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionvoidappendBit(boolean bit) Appends a single bit to the end of the array.voidappendBitArray(BitArray other) Appends another BitArray to the end of this array.voidappendBits(int value, int numBits) Appends the least-significant bits, from value, in order from most-significant to least-significant.voidclear()Clears all bits (sets to false).clone()A copy of this array.private voidensureCapacity(int size) Grows the storage if it is too small.booleanWhether the other array has the same bits.voidflip(int i) Flips bit i.booleanget(int i) Gets the bit at the specified index.int[]Returns the underlying array of ints.intgetNextSet(int from) Gets the index of the next set bit.intgetNextUnset(int from) Gets the index of the next unset bit.intgetSize()Get the number of bits in the array.intGet the size of the array in bytes.inthashCode()Hash of the bit words.booleanisRange(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) Allocates enough ints to hold a number of bits.voidreverse()Reverses all bits in the array.voidset(int i) Sets bit i.voidsetBulk(int i, int newBits) Sets a block of 32 bits, starting at bit i.voidsetRange(int start, int end) Sets a range of bits.voidtoBytes(int bitOffset, byte[] array, int offset, int numBytes) Writes the bits to an array of bytes.toString()Bits as text for debugging.voidPerforms a bitwise XOR of this array with another.
Field Details
bits
private int[] bitsThe bits, packed into ints.size
private int sizeHow many bits are stored.
Constructor Details
BitArray
public BitArray()Creates a new empty BitArray with zero size.BitArray
public BitArray(int size) Creates a new BitArray with the specified size.- Parameters:
size- the number of bits in the array
BitArray
BitArray(int[] bits, int size) For testing only.- Parameters:
bits- the bit array datasize- the number of bits
Method Details
getSize
public int getSize()Get the number of bits in the array.- Returns:
- the number of bits in the array
getSizeInBytes
public int getSizeInBytes()Get the size of the array in bytes.- Returns:
- the size of the array in bytes (rounded up)
ensureCapacity
private void ensureCapacity(int size) Grows the storage if it is too small.- Parameters:
size- the number of bits needed
get
public boolean get(int i) Gets the bit at the specified index.- Parameters:
i- bit to get- Returns:
- true iff bit i is set
set
public void set(int i) Sets bit i.- Parameters:
i- bit to set
flip
public void flip(int i) Flips bit i.- Parameters:
i- bit to set
getNextSet
public int getNextSet(int from) Gets the index of the next set bit.- Parameters:
from- first bit to check- Returns:
- index of first bit that is set, starting from the given index, or size if none are set at or beyond this given index
- See Also:
getNextUnset
public int getNextUnset(int from) Gets the index of the next unset bit.- Parameters:
from- index to start looking for unset bit- Returns:
- index of next unset bit, or
sizeif none are unset until the end - See Also:
setBulk
public void setBulk(int i, int newBits) Sets a block of 32 bits, starting at bit i.- Parameters:
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.
setRange
public void setRange(int start, int end) Sets a range of bits.- Parameters:
start- start of range, inclusive.end- end of range, exclusive
clear
public void clear()Clears all bits (sets to false).isRange
public boolean isRange(int start, int end, boolean value) Efficient method to check if a range of bits is set, or not set.- Parameters:
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 set- Returns:
- true iff all bits are set or not set in range, according to value argument
- Throws:
IllegalArgumentException- if end is less than start or the range is not contained in the array
appendBit
public void appendBit(boolean bit) Appends a single bit to the end of the array.- Parameters:
bit- the bit value to append.
appendBits
public void appendBits(int value, int numBits) Appends the least-significant bits, from value, in order from most-significant to least-significant. For example, appending 6 bits from 0x000001E will append the bits 0, 1, 1, 1, 1, 0 in that order.- Parameters:
value-intcontaining bits to appendnumBits- bits from value to append
appendBitArray
Appends another BitArray to the end of this array.- Parameters:
other- the BitArray to append
xor
Performs a bitwise XOR of this array with another.- Parameters:
other- the BitArray to XOR with- Throws:
IllegalArgumentException- if sizes don't match
toBytes
public void toBytes(int bitOffset, byte[] array, int offset, int numBytes) Writes the bits to an array of bytes.- Parameters:
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 bygetBitArray()offset- position in array to start writingnumBytes- how many bytes to write
getBitArray
public int[] getBitArray()Returns the underlying array of ints.- Returns:
- underlying array of ints. The first element holds the first 32 bits, and the least significant bit is bit 0.
reverse
public void reverse()Reverses all bits in the array.makeArray
private static int[] makeArray(int size) Allocates enough ints to hold a number of bits.- Parameters:
size- the number of bits- Returns:
- the storage to hold them
equals
hashCode
toString
clone