Package net.emustudio.edigen.misc
Class BitSequence
- java.lang.Object
-
- net.emustudio.edigen.misc.BitSequence
-
public class BitSequence extends java.lang.ObjectA sequence of bits with defined but modifiable length. UnlikeBitSet, where the length is determined by the position of the highest bit set to 1, this sequence has an explicitly defined bit count, so it can end with zeroes and the length is preserved. The sequence length can be modified after creation. In addition, several conversion methods are provided.
-
-
Constructor Summary
Constructors Constructor Description BitSequence()Constructs a bit sequence with zero length.BitSequence(int length)Constructs a bit sequence with the specified length.BitSequence(int length, boolean value)Constructs a bit sequence with the specified length and fills it with the specified value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BitSequenceand(BitSequence other)Returns a bit sequence ANDed with an other bit sequence.voidappend(boolean bit)Appends one bit to the end of this sequence.voidappend(BitSequence bits)Appends another bit sequence to the end of this sequence.booleancontainsOnly(boolean value)Returns true if the sequence contains only bits of the specified value.booleanequals(java.lang.Object object)Checks whether the other bit sequence has exactly the same content and length as this sequence.static BitSequencefromBinary(java.lang.String binaryString)Returns a new instance of bit sequence with the length and content obtained from the binary number represented as a string.static BitSequencefromHexadecimal(java.lang.String hexString)Returns a new instance of bit sequence with the length and content obtained from the hexadecimal number represented as a string.booleanget(int index)Returns the bit at the given index.intgetLength()Returns the total length of this bit sequence.inthashCode()Returns the hash code of this sequence.voidset(int index, boolean value)Sets the bit at the given index to the specified value.BitSequence[]split(int bitsPerPiece)Splits the sequence into shorter sequences of equal length.BitSequencesubSequence(int start, int length)Returns the subsequence of the current bit sequence.boolean[]toBooleanArray()Returns this sequence as an array of boolean values.java.lang.StringtoHexadecimal()Returns the hexadecimal representation of this sequence.java.lang.StringtoString()Returns a string representation of the object.
-
-
-
Constructor Detail
-
BitSequence
public BitSequence()
Constructs a bit sequence with zero length.
-
BitSequence
public BitSequence(int length)
Constructs a bit sequence with the specified length. The bit sequence is initially filled withfalsevalues.- Parameters:
length- the initial sequence length, in bits
-
BitSequence
public BitSequence(int length, boolean value)Constructs a bit sequence with the specified length and fills it with the specified value.- Parameters:
length- the initial sequence length, in bitsvalue- the value to fill the sequence with
-
-
Method Detail
-
fromBinary
public static BitSequence fromBinary(java.lang.String binaryString)
Returns a new instance of bit sequence with the length and content obtained from the binary number represented as a string. Leading zeroes are included in the resulting sequence.- Parameters:
binaryString- the string representation of the binary number; can contain only characters '0' and '1'- Returns:
- the constructed bit sequence
- Throws:
java.lang.NumberFormatException- if the input contains invalid characters
-
fromHexadecimal
public static BitSequence fromHexadecimal(java.lang.String hexString)
Returns a new instance of bit sequence with the length and content obtained from the hexadecimal number represented as a string. Leading zeroes are included in the resulting sequence.- Parameters:
hexString- the string representation of the hexedecimal number; can contain only characters '0' - '9', 'a' - 'f' (or uppercase)- Returns:
- the constructed bit sequence
- Throws:
java.lang.NumberFormatException- if the input contains invalid characters
-
get
public boolean get(int index)
Returns the bit at the given index.- Parameters:
index- the index to read, starting at zero- Returns:
- the bit as boolean
- Throws:
java.lang.IndexOutOfBoundsException- when index is not in [0; length)
-
set
public void set(int index, boolean value)Sets the bit at the given index to the specified value.- Parameters:
index- the index to modifyvalue- the boolean value
-
getLength
public int getLength()
Returns the total length of this bit sequence.- Returns:
- the length, in bits
-
containsOnly
public boolean containsOnly(boolean value)
Returns true if the sequence contains only bits of the specified value. If the sequence length is zero,trueis returned.- Parameters:
value- the expected bit value- Returns:
- true if the bit sequence contains only given bits, false otherwise
-
append
public void append(BitSequence bits)
Appends another bit sequence to the end of this sequence.- Parameters:
bits- the sequence to be appended
-
append
public void append(boolean bit)
Appends one bit to the end of this sequence.- Parameters:
bit- the value to be appended
-
split
public BitSequence[] split(int bitsPerPiece)
Splits the sequence into shorter sequences of equal length.If it is necessary, the last sequence in the returned array will contain less than 8 * bytesPerPiece bits.
For a zero-length sequence, an array containing one empty sequence is returned.
- Parameters:
bitsPerPiece- the number of bits in each sequence- Returns:
- the array of shorter sequences
-
subSequence
public BitSequence subSequence(int start, int length)
Returns the subsequence of the current bit sequence.- Parameters:
start- the index of the first bit, includedlength- the length of the subsequence, in bits- Returns:
- the subsequence
- Throws:
java.lang.IndexOutOfBoundsException- if the resulting subsequence exceeded the sequence boundary
-
and
public BitSequence and(BitSequence other)
Returns a bit sequence ANDed with an other bit sequence.- Parameters:
other- the second sequence- Returns:
- the result, truncated to the length of the shorter input sequence
-
equals
public boolean equals(java.lang.Object object)
Checks whether the other bit sequence has exactly the same content and length as this sequence.- Overrides:
equalsin classjava.lang.Object- Parameters:
object- the sequence to compare- Returns:
- true if the sequences are same, false otherwise
-
hashCode
public int hashCode()
Returns the hash code of this sequence. The algorithm was automatically generated by NetBeans IDE.- Overrides:
hashCodein classjava.lang.Object- Returns:
- the computed hash code
-
toString
public java.lang.String toString()
Returns a string representation of the object.- Overrides:
toStringin classjava.lang.Object- Returns:
- the string
-
toBooleanArray
public boolean[] toBooleanArray()
Returns this sequence as an array of boolean values.- Returns:
- the boolean array
-
toHexadecimal
public java.lang.String toHexadecimal()
Returns the hexadecimal representation of this sequence. Before computation, the sequence is zero-padded to whole nibbles from the beginning.- Returns:
- the hexadecimal string, in lowercase
-
-