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 givendata
array.static byte[]
copy
(byte[] data, int start, int end) Copies the specified range of the specified array into a new array.static int
count
(byte value) Returns the number of one-bits in the givenbyte
value
.static int
count
(byte[] data) Returns the number of one-bits in the givenbyte[]
array.static int
count
(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 boolean
get
(byte[] data, int index) Return the (boolean) value of the byte array at the given bit index.static boolean
getAndSet
(byte[] array, int index) static byte[]
increment
(byte[] data) Increment the givendata
array.static byte[]
invert
(byte[] data) Invert the givendata
array.static byte[]
newArray
(int length) Create a newbyte[]
array which can store at least the number of bits as defined by the givenlength
parameter.static byte[]
newArray
(int length, double p) Create a newbyte[]
array which can store at least the number of bits as defined by the givenlength
parameter.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 givendata
array the givenshift
to the left.static byte[]
shiftRight
(byte[] data, int shift) Shifting all bits in the givendata
array the givenshift
to the right.static void
swap
(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 int
toByteLength
(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 String
toByteString
(byte... data) Convert a binary representation of the given byte array to a string.static int
toInt
(byte[] data) static long
toLong
(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 thedata
array 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 thedata
array 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 thedata
array 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 thedata
array 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 thedata
byte array, inclusively.end
- the end bit index of thedata
byte array, exclusively.otherData
- the other byte array to swap the elements with.otherStart
- the start index of theotherData
byte array.- Throws:
IndexOutOfBoundsException
- ifstart > end
or 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
- thebyte
array for which the one bits should be counted.- Returns:
- the number of one bits in the given
byte
array.
-
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
byte
array.
-
count
Returns the number of one-bits in the givenbyte
value
.- 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 givendata
array the givenshift
to 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
data
array. - Throws:
NullPointerException
- if thedata
array isnull
.
-
shiftLeft
Shifting all bits in the givendata
array the givenshift
to 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
data
array. - Throws:
NullPointerException
- if thedata
array isnull
.
-
increment
Increment the givendata
array.- Parameters:
data
- the givendata
array.- Returns:
- the given
data
array. - Throws:
NullPointerException
- if thedata
array isnull
.
-
invert
Invert the givendata
array.- Parameters:
data
- the givendata
array.- Returns:
- the given
data
array. - Throws:
NullPointerException
- if thedata
array isnull
.
-
complement
Make the two's complement of the givendata
array.- Parameters:
data
- the givendata
array.- Returns:
- the given
data
array. - Throws:
NullPointerException
- if thedata
array 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 thedata
array 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 thedata
array 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 givenlength
parameter.- 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 givenlength
parameter. 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
- ifp
is 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
-