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 SummaryModifier 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- 
lengthReturn the length of the bit-array.- Returns:
- the length of the bit array
 
- 
bitCountReturn the number of set bits of this bit-array.- Returns:
- the number of set bits
 
- 
setSets the specified bitvalueat the given bitindex.- Parameters:
- index- the bit index
- value- the bit value
- Throws:
- IndexOutOfBoundsException- if the index is not within the valid range of- [0, length())
 
- 
setSet 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())
 
- 
unsetSet 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())
 
- 
getReturn 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())
 
- 
invertInvertsthisbit-array.
- 
shiftLeftShifting 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
 
- 
shiftRightShifting 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
 
- 
signumReturn 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)
 
- 
toBigIntegerReturn 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:
 
- 
toByteArrayReturn 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
 
- 
copyCreate a new copy ofthisbit-array.
- 
hashCode
- 
equals
- 
toString
- 
toByteStringConvert 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.
 
- 
ofCreates 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 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 value
- length- the length of the created bit-array
- Returns:
- a newly created bit-array which represent the given value
- Throws:
- NullPointerException- if the given- valueis- null
- NegativeArraySizeException- if the- lengthis negative
- See Also:
 
- 
ofCreate 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 given- valueis- null
- See Also:
 
- 
ofCreates 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's
- length- the length of the created bit-array
- Returns:
- a new bit-array from the given input value
- Throws:
- IllegalArgumentException- if the given input- valueis empty
- See Also:
 
- 
ofCreates 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 input- valueis empty
- See Also:
 
- 
ofCreate a new bit-array with the givendatavalues andbeginandendbit indexes. The givendatais copied.- Parameters:
- data- the- byte[]array which contains the bit data
- begin- the start bit index (inclusively)
- end- the end bit index (exclusively)
- Returns:
- a newly created bit-array
- Throws:
- NullPointerException- if the given- dataarray is- null
- IllegalArgumentException- if the- beginand- endindexes are not within the valid range
 
- 
ofCreate a new bit-array with the givendatavalues andlength. The givendatais copied.- Parameters:
- data- the- byte[]array which contains the bit data
- length- the bit length
- Returns:
- a newly created bit-array
- Throws:
- NullPointerException- if the given- dataarray is- null
- IllegalArgumentException- if the- lengthis greater than- data.length*Byte.SIZE
 
- 
ofCreate a new bit-array with the givendatavalues andlength. The givendatais copied.- Parameters:
- data- the- byte[]array which contains the bit data
- Returns:
- a newly created bit-array
- Throws:
- NullPointerException- if the given- dataarray is- null
 
- 
ofLengthCrate 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 given- lengthis smaller then one
 
- 
ofLengthCreate 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- if- pis not a valid probability.
 
 
-