Class BitSequence


  • public class BitSequence
    extends java.lang.Object
    A sequence of bits with defined but modifiable length. Unlike BitSet, 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
      BitSequence and​(BitSequence other)
      Returns a bit sequence ANDed with an other bit sequence.
      void append​(boolean bit)
      Appends one bit to the end of this sequence.
      void append​(BitSequence bits)
      Appends another bit sequence to the end of this sequence.
      boolean containsOnly​(boolean value)
      Returns true if the sequence contains only bits of the specified value.
      boolean equals​(java.lang.Object object)
      Checks whether the other bit sequence has exactly the same content and length as this sequence.
      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.
      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.
      boolean get​(int index)
      Returns the bit at the given index.
      int getLength()
      Returns the total length of this bit sequence.
      int hashCode()
      Returns the hash code of this sequence.
      void set​(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.
      BitSequence subSequence​(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.String toHexadecimal()
      Returns the hexadecimal representation of this sequence.
      java.lang.String toString()
      Returns a string representation of the object.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • 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 with false values.
        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 bits
        value - 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 modify
        value - 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, true is 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, included
        length - 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:
        equals in class java.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:
        hashCode in class java.lang.Object
        Returns:
        the computed hash code
      • toString

        public java.lang.String toString()
        Returns a string representation of the object.
        Overrides:
        toString in class java.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