java.lang.Object
io.jenetics.internal.util.Bits
Some bit utils. All operations assume
little-endian byte order.
Byte: 3 2 1 0
| | | |
Array: |11110011|10011101|01000000|00101010|
| | | |
Bit: 23 15 7 0
- Since:
- 1.0
- Version:
- 5.2
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]complement(byte[] data) Make the two's complement of the givendataarray.static byte[]copy(byte[] data, int start, int end) Copies the specified range of the specified array into a new array.static intcount(byte value) Returns the number of one-bits in the givenbytevalue.static intcount(byte[] data) Returns the number of one-bits in the givenbyte[]array.static intcount(byte[] bits, int start, int end) Returns the number of one-bits in the givenbyte[]array.static byte[]flip(byte[] data, int index) Flip the bit at the given index.static byte[]fromByteString(String data) Convert a string which was created with thetoByteString(byte...)method back to a byte array.static booleanget(byte[] data, int index) Return the (boolean) value of the byte array at the given bit index.static booleangetAndSet(byte[] array, int index) static byte[]increment(byte[] data) Increment the givendataarray.static byte[]invert(byte[] data) Invert the givendataarray.static byte[]newArray(int length) Create a newbyte[]array which can store at least the number of bits as defined by the givenlengthparameter.static byte[]newArray(int length, double p) Create a newbyte[]array which can store at least the number of bits as defined by the givenlengthparameter.static byte[]reverse(byte[] array) static byte[]set(byte[] data, int index) Set the bit in the given byte array at the bit position (not the index within the byte array) totrue.static byte[]set(byte[] data, int index, boolean value) Set the bit in the given byte array at the bit position (not the index within the byte array) to the specified value.static byte[]shiftLeft(byte[] data, int shift) Shifting all bits in the givendataarray the givenshiftto the left.static byte[]shiftRight(byte[] data, int shift) Shifting all bits in the givendataarray the givenshiftto the right.static voidswap(byte[] data, int start, int end, byte[] otherData, int otherStart) Swap a given range with a range of the same size with another array.static inttoByteLength(int bitLength) Return the minimum number of bytes to store the given number of bits.static byte[]toBytes(int value) static byte[]toBytes(long value) static StringtoByteString(byte... data) Convert a binary representation of the given byte array to a string.static inttoInt(byte[] data) static longtoLong(byte[] data) static byte[]unset(byte[] data, int index) Set the bit in the given byte array at the bit position (not the index within the byte array) tofalse.
-
Method Details
-
get
Return the (boolean) value of the byte array at the given bit index.- Parameters:
data- the byte array.index- the bit index.- Returns:
- the value at the given bit index.
- Throws:
IndexOutOfBoundsException- if the index isindex >= max || index < 0.NullPointerException- if thedataarray isnull.
-
set
Set the bit in the given byte array at the bit position (not the index within the byte array) to the specified value.- Parameters:
data- the byte array.index- the bit index within the byte array.value- the value to set.- Returns:
- the given data array.
- Throws:
IndexOutOfBoundsException- if the index isindex >= max || index < 0.NullPointerException- if thedataarray isnull.
-
set
Set the bit in the given byte array at the bit position (not the index within the byte array) totrue.- Parameters:
data- the byte array.index- the bit index within the byte array.- Returns:
- the given data array.
- Throws:
IndexOutOfBoundsException- if the index isindex >= max || index < 0.NullPointerException- if thedataarray isnull.
-
unset
Set the bit in the given byte array at the bit position (not the index within the byte array) tofalse.- Parameters:
data- the byte array.index- the bit index within the byte array.- Returns:
- the given data array.
- Throws:
IndexOutOfBoundsException- if the index isindex >= max || index < 0.NullPointerException- if thedataarray isnull.
-
swap
Swap a given range with a range of the same size with another array.start end | | data: +---+---+---+---+---+---+---+---+---+---+---+---+ +---------------+ +---------------+ otherData: +---+---+---+---+---+---+---+---+---+---+---+---+ | otherStart- Parameters:
data- the first byte array which is used for swapping.start- the start bit index of thedatabyte array, inclusively.end- the end bit index of thedatabyte array, exclusively.otherData- the other byte array to swap the elements with.otherStart- the start index of theotherDatabyte array.- Throws:
IndexOutOfBoundsException- ifstart > endor ifstart < 0 || end >= data.length*8 || otherStart < 0 || otherStart + (end - start) >= otherData.length*8
-
count
Returns the number of one-bits in the givenbyte[]array.- Parameters:
data- thebytearray for which the one bits should be counted.- Returns:
- the number of one bits in the given
bytearray.
-
count
Returns the number of one-bits in the givenbyte[]array.- Parameters:
bits- the bit values of the new chromosome gene.start- the initial (bit) index of the range to be copied, inclusiveend- the final (bit) index of the range to be copied, exclusive. (This index may lie outside the array.)- Returns:
- the number of one-bits in the given
bytearray.
-
count
Returns the number of one-bits in the givenbytevalue.- Parameters:
value- the value for which the one bits should be counted.- Returns:
- the number of one bits in the given value
-
shiftRight
Shifting all bits in the givendataarray the givenshiftto the right. The bits on the left side are filled with zeros.- Parameters:
data- the data bits to shift.shift- the number of bits to shift.- Returns:
- the given
dataarray. - Throws:
NullPointerException- if thedataarray isnull.
-
shiftLeft
Shifting all bits in the givendataarray the givenshiftto the left. The bits on the right side are filled with zeros.- Parameters:
data- the data bits to shift.shift- the number of bits to shift.- Returns:
- the given
dataarray. - Throws:
NullPointerException- if thedataarray isnull.
-
increment
Increment the givendataarray.- Parameters:
data- the givendataarray.- Returns:
- the given
dataarray. - Throws:
NullPointerException- if thedataarray isnull.
-
invert
Invert the givendataarray.- Parameters:
data- the givendataarray.- Returns:
- the given
dataarray. - Throws:
NullPointerException- if thedataarray isnull.
-
complement
Make the two's complement of the givendataarray.- Parameters:
data- the givendataarray.- Returns:
- the given
dataarray. - Throws:
NullPointerException- if thedataarray isnull.
-
flip
Flip the bit at the given index.- Parameters:
data- the data array.index- the index of the bit to flip.- Returns:
- the input array, for command chaining
- Throws:
IndexOutOfBoundsException- if the index isindex >= max || index < 0.NullPointerException- if thedataarray isnull.
-
reverse
-
copy
Copies the specified range of the specified array into a new array.- Parameters:
data- the bits from which a range is to be copiedstart- the initial index of the range to be copied, inclusiveend- the final index of the range to be copied, exclusive.- Returns:
- a new array containing the specified range from the original array
- Throws:
ArrayIndexOutOfBoundsException- if start < 0 or start > data.length*8IllegalArgumentException- if start > endNullPointerException- if thedataarray isnull.
-
getAndSet
-
toByteString
Convert a binary representation of the given byte array to a string. The string has the following format:Only the array string is printed.Byte: 3 2 1 0 | | | | Array: "11110011|10011101|01000000|00101010" | | | | Bit: 23 15 7 0- Parameters:
data- the byte array to convert to a string.- Returns:
- the binary representation of the given byte array.
- See Also:
-
fromByteString
Convert a string which was created with thetoByteString(byte...)method back to a byte array.- Parameters:
data- the string to convert.- Returns:
- the byte array.
- Throws:
IllegalArgumentException- if the given data string could not be converted.- See Also:
-
newArray
Create a newbyte[]array which can store at least the number of bits as defined by the givenlengthparameter.- Parameters:
length- the number of bits, the returned byte array can store.- Returns:
- the new byte array.s
-
newArray
Create a newbyte[]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 byte 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.
-
toByteLength
Return the minimum number of bytes to store the given number of bits.- Parameters:
bitLength- the number of bits- Returns:
- the number of bytes needed to store the given number of bits.
-
toInt
-
toBytes
-
toLong
-
toBytes
-