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 TypeMethodDescriptionint
bitCount()
Return the number of set bits of this bit-array.copy()
Create a new copy ofthis
bit-array.boolean
boolean
get
(int index) Return the bit value at the given bitindex
.int
hashCode()
void
invert()
Invertsthis
bit-array.int
length()
Return the length of the bit-array.static BitArray
of
(byte[] data) Create a new bit-array with the givendata
values andlength
.static BitArray
of
(byte[] data, int length) Create a new bit-array with the givendata
values andlength
.static BitArray
of
(byte[] data, int begin, int end) Create a new bit-array with the givendata
values andbegin
andend
bit indexes.static BitArray
of
(CharSequence value) Creates a new bit-array from the given stringvalue
.static BitArray
of
(CharSequence value, int length) Creates a new bit-array from the given stringvalue
.static BitArray
of
(BigInteger value) Create a new bit-array from the givenBigInteger
value.static BitArray
of
(BigInteger value, int length) Creates a new bit-array from the givenvalue
and the givenlength
.static BitArray
ofLength
(int length) Crate a new bit-array with the given length.static BitArray
ofLength
(int length, double p) Create a new bit-array which can store at least the number of bits as defined by the givenlength
parameter.void
set
(int index) Set the bit in the given byte array at the bit position (not the index within the byte array) totrue
.void
set
(int index, boolean value) Sets the specified bitvalue
at the given bitindex
.void
shiftLeft
(int n) Shifting all bits inthis
bit array the givenn
bits to the left.void
shiftRight
(int n) Shifting all bits inthis
bit array the givenn
bits to the right.int
signum()
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 asBigInteger
value.byte[]
Return thebyte[]
array, which represents the state of the state ofthis
bit-array.Convert a binary representation ofthis
bit-array to a string.toString()
void
unset
(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 bitvalue
at 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
Invertsthis
bit-array. -
shiftLeft
Shifting all bits inthis
bit array the givenn
bits 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 inthis
bit array the givenn
bits 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 asBigInteger
value. This bit-array can be recreated by the returnedBigInteger
value. But only with the samelength()
ofthis
bit-array.final var bits = BitArray.of("1111111010100110010110110010011110110101"); final var bint = bits.toBigInteger(); assert BitArray.of(bint, bits.length()).equals(bits);
- Returns:
- a new
BigInteger
object, which represents the integer value of this bit-array - See Also:
-
toByteArray
Return thebyte[]
array, which represents the state of the state ofthis
bit-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 ofthis
bit-array. -
hashCode
-
equals
-
toString
-
toByteString
Convert a binary representation ofthis
bit-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
this
bit array.
-
of
Creates a new bit-array from the givenvalue
and the givenlength
. It is guaranteed that the created bit-array will represent the givenBigInteger
, as long as thelength
is big enough to store the whole value. If the length is shorter than 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 givenvalue
isnull
NegativeArraySizeException
- if thelength
is negative- See Also:
-
of
Create a new bit-array from the givenBigInteger
value.- Parameters:
value
- the integer value- Returns:
- a newly created bit-array which represent the given
value
- Throws:
NullPointerException
- if the givenvalue
isnull
- See Also:
-
of
Creates a new bit-array from the given stringvalue
. The givenlength
might be bigger and smaller than the length of the givenvalue
string. The higher order bits of the created bit-array are trimmed or filled with zero if thelength
is 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 inputvalue
is 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 inputvalue
is empty- See Also:
-
of
Create a new bit-array with the givendata
values andbegin
andend
bit indexes. The givendata
is 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 givendata
array isnull
IllegalArgumentException
- if thebegin
andend
indexes are not within the valid range
-
of
Create a new bit-array with the givendata
values andlength
. The givendata
is copied.- Parameters:
data
- thebyte[]
array which contains the bit datalength
- the bit length- Returns:
- a newly created bit-array
- Throws:
NullPointerException
- if the givendata
array isnull
IllegalArgumentException
- if thelength
is greater thandata.length*Byte.SIZE
-
of
Create a new bit-array with the givendata
values andlength
. The givendata
is copied.- Parameters:
data
- thebyte[]
array which contains the bit data- Returns:
- a newly created bit-array
- Throws:
NullPointerException
- if the givendata
array 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 givenlength
is smaller then one
-
ofLength
Create a new bit-array which can store at least the number of bits as defined by the givenlength
parameter. 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
- ifp
is not a valid probability.
-