java.lang.Object
BitArray
This class represents a fixed sized array of bit or boolean
values, backed by a
byte[] array. The order of the bit values is shown
if the drawing.
Byte: 3 2 1 0
| | | |
Array: |11110011|10011101|01000000|00101010|
| | | |
Bit: 23 15 7 0
- Since:
- 7.0
- Version:
- 7.0
-
Method Summary
Modifier and TypeMethodDescriptionintbitCount()Return the number of set bits of this bit-array.copy()Create a new copy ofthisbit-array.booleanbooleanget(int index) Return the bit value at the given bitindex.inthashCode()voidinvert()Invertsthisbit-array.intlength()Return the length of the bit-array.static BitArrayof(byte[] data) Create a new bit-array with the givendatavalues andlength.static BitArrayof(byte[] data, int length) Create a new bit-array with the givendatavalues andlength.static BitArrayof(byte[] data, int begin, int end) Create a new bit-array with the givendatavalues andbeginandendbit indexes.static BitArrayof(CharSequence value) Creates a new bit-array from the given stringvalue.static BitArrayof(CharSequence value, int length) Creates a new bit-array from the given stringvalue.static BitArrayof(BigInteger value) Create a new bit-array from the givenBigIntegervalue.static BitArrayof(BigInteger value, int length) Creates a new bit-array from the givenvalueand the givenlength.static BitArrayofLength(int length) Crate a new bit-array with the given length.static BitArrayofLength(int length, double p) Create a new bit-array which can store at least the number of bits as defined by the givenlengthparameter.voidset(int index) Set the bit in the given byte array at the bit position (not the index within the byte array) totrue.voidset(int index, boolean value) Sets the specified bitvalueat the given bitindex.voidshiftLeft(int n) Shifting all bits inthisbit array the givennbits to the left.voidshiftRight(int n) Shifting all bits inthisbit array the givennbits to the right.intsignum()Return the signum of the number, represented by this bit-array (-1 for negative, 0 for zero, 1 for positive).Return the value of this bit-array asBigIntegervalue.byte[]Return thebyte[]array, which represents the state of the state ofthisbit-array.Convert a binary representation ofthisbit-array to a string.toString()voidunset(int index) Set the bit in the given byte array at the bit position (not the index within the byte array) tofalse.
-
Method Details
-
length
Return the length of the bit-array.- Returns:
- the length of the bit array
-
bitCount
Return the number of set bits of this bit-array.- Returns:
- the number of set bits
-
set
Sets the specified bitvalueat the given bitindex.- Parameters:
index- the bit indexvalue- the bit value- Throws:
IndexOutOfBoundsException- if the index is not within the valid range of[0, length())
-
set
Set the bit in the given byte array at the bit position (not the index within the byte array) totrue.- Parameters:
index- the bit index- Throws:
IndexOutOfBoundsException- if the index is not within the valid range of[0, length())
-
unset
Set the bit in the given byte array at the bit position (not the index within the byte array) tofalse.- Parameters:
index- the bit index- Throws:
IndexOutOfBoundsException- if the index is not within the valid range of[0, length())
-
get
Return the bit value at the given bitindex.- Parameters:
index- the bit index- Returns:
- the bit value
- Throws:
IndexOutOfBoundsException- if the index is not within the valid range of[0, length())
-
invert
Invertsthisbit-array. -
shiftLeft
Shifting all bits inthisbit array the givennbits to the left. The bits on the right side are filled with zeros.- Parameters:
n- the number of bits to shift.- Since:
- 7.1
-
shiftRight
Shifting all bits inthisbit array the givennbits to the right. The bits on the right side are filled with zeros.- Parameters:
n- the number of bits to shift.- Since:
- 7.1
-
signum
Return the signum of the number, represented by this bit-array (-1 for negative, 0 for zero, 1 for positive).final BitArray bits = ...; final BigInteger i = bits.toBigInteger(); assert bits.signum() == i.signum();- Returns:
- the signum of the number, represented by this bit-array (-1 for negative, 0 for zero, 1 for positive)
-
toBigInteger
Return the value of this bit-array asBigIntegervalue. This bit-array can be recreated by the returnedBigIntegervalue. But only with the samelength()ofthisbit-array.final var bits = BitArray.of("1111111010100110010110110010011110110101"); final var bint = bits.toBigInteger(); assert BitArray.of(bint, bits.length()).equals(bits);- Returns:
- a new
BigIntegerobject, which represents the integer value of this bit-array - See Also:
-
toByteArray
Return thebyte[]array, which represents the state of the state ofthisbit-array.final BitArray bits = ...; final byte[] bytes = bits.toByteArray(); assert bits.equals(BitArray.of(bytes, bits.length()));- Returns:
- the bit-array data as
byte[]array
-
copy
Create a new copy ofthisbit-array. -
hashCode
-
equals
-
toString
-
toByteString
Convert a binary representation ofthisbit-array to a string. The string has the following format:Byte: 3 2 1 0 | | | | Array: "11110011|10011101|01000000|00101010" | | | | Bit: 23 15 7 0- Returns:
- the binary representation of
thisbit array.
-
of
Creates a new bit-array from the givenvalueand the givenlength. It is guaranteed, that the created bit-array will represent the givenBigInteger, as long as thelengthis big enough to store the whole value. If the length is shorter then required, the higher order bits will be truncated.final var length = 2048; final var bint = BigInteger.probablePrime(length, new Random()); final var bits = BitArray.of(bint, length + 1); assert bits3.toBigInteger().equals(bint);- Parameters:
value- the integer valuelength- the length of the created bit-array- Returns:
- a newly created bit-array which represent the given
value - Throws:
NullPointerException- if the givenvalueisnullNegativeArraySizeException- if thelengthis negative- See Also:
-
of
Create a new bit-array from the givenBigIntegervalue.- Parameters:
value- the integer value- Returns:
- a newly created bit-array which represent the given
value - Throws:
NullPointerException- if the givenvalueisnull- See Also:
-
of
Creates a new bit-array from the given stringvalue. The givenlengthmight be bigger and smaller than the length of the givenvaluestring. The higher order bits of the created bit-array are trimmed or filled with zero if thelengthis smaller or bigger than the given string.- Parameters:
value- the given input string, consisting only of '0's and '1'slength- the length of the created bit-array- Returns:
- a new bit-array from the given input
value - Throws:
IllegalArgumentException- if the given inputvalueis empty- See Also:
-
of
Creates a new bit-array from the given stringvalue. The string, created by thetoString()method, will be equals to the given inputvalue.final var string = "11111110101001100101101100100111101101011101"; final var bits = BitArray.of(string); assert bits.toString().equals(string);- Parameters:
value- the given input string, consisting only of '0's and '1's- Returns:
- a new bit-array from the given input
value - Throws:
IllegalArgumentException- if the given inputvalueis empty- See Also:
-
of
Create a new bit-array with the givendatavalues andbeginandendbit indexes. The givendatais copied.- Parameters:
data- thebyte[]array which contains the bit databegin- the start bit index (inclusively)end- the end bit index (exclusively)- Returns:
- a newly created bit-array
- Throws:
NullPointerException- if the givendataarray isnullIllegalArgumentException- if thebeginandendindexes are not within the valid range
-
of
Create a new bit-array with the givendatavalues andlength. The givendatais copied.- Parameters:
data- thebyte[]array which contains the bit datalength- the bit length- Returns:
- a newly created bit-array
- Throws:
NullPointerException- if the givendataarray isnullIllegalArgumentException- if thelengthis greater thandata.length*Byte.SIZE
-
of
Create a new bit-array with the givendatavalues andlength. The givendatais copied.- Parameters:
data- thebyte[]array which contains the bit data- Returns:
- a newly created bit-array
- Throws:
NullPointerException- if the givendataarray isnull
-
ofLength
Crate a new bit-array with the given length. All bits are set to '0'.- Parameters:
length- the length of the bit-array- Returns:
- a newly created bit-array with the given
length - Throws:
IllegalArgumentException- if the givenlengthis smaller then one
-
ofLength
Create a new bit-array which can store at least the number of bits as defined by the givenlengthparameter. The returned byte array is initialized with ones according to the given ones probabilityp.- Parameters:
length- the number of bits, the returned bit-array can store.p- the ones probability of the returned byte array.- Returns:
- the new byte array.s
- Throws:
IllegalArgumentException- ifpis not a valid probability.
-