Class BitString

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static BitString EMPTY_STRING
      A null MutableString
    • Constructor Summary

      Constructors 
      Constructor Description
      BitString​(byte[] bytes)
      Creates a BitString from a byte[].
      BitString​(int length)
      Creates a BitString with a specific length (length is the number of bits).
    • Constructor Detail

      • BitString

        public BitString​(int length)
        Creates a BitString with a specific length (length is the number of bits).
        Parameters:
        length - The BitString length (it's a number of bits)
      • BitString

        public BitString​(byte[] bytes)
        Creates a BitString from a byte[]. As the first byte is the number of unused bits in the last byte, we have to ignore it.
        Parameters:
        bytes - The value to store. The first byte contains the number of unused bits
    • Method Detail

      • setData

        public void setData​(byte[] data)
        Set a new BitString in the BitString. It will replace the old BitString, and reset the current length with the new one.
        Parameters:
        data - The string to store
      • getData

        public byte[] getData()
        Get the representation of a BitString. A first byte containing the number of unused bits is added
        Returns:
        A byte array which represent the BitString
      • getUnusedBits

        public byte getUnusedBits()
        Get the number of unused bits
        Returns:
        A byte which represent the number of unused bits
      • setBit

        public void setBit​(int pos)
        Set a bit at a specified position. The bits are stored from left to right. For instance, if we have 10 bits, then they are coded as b0 b1 b2 b3 b4 b5 b6 b7 - b8 b9 x x x x x x
        Parameters:
        pos - The bit to set
      • clearBit

        public void clearBit​(int pos)
        Clear a bit at a specified position. The bits are stored from left to right. For instance, if we have 10 bits, then they are coded as b0 b1 b2 b3 b4 b5 b6 b7 - b8 b9 x x x x x x
        Parameters:
        pos - The bit to clear
      • getBit

        public boolean getBit​(int pos)
        Get the bit stored into the BitString at a specific position. The bits are stored from left to right, the LSB on the left and the MSB on the right.
        For instance, if we have 10 bits, then they are coded as b0 b1 b2 b3 - b4 b5 b6 b7 - b8 b9 x x - x x x x
         With '1001 000x', where x is an unused bit,
               ^ ^    ^
               | |    |
               | |    |
               | |    +----- getBit(6) = 0
               | +---------- getBit(2) = 0
               +------------ getBit(0) = 1
         
        Parameters:
        pos - The position of the requested bit.
        Returns:
        true if the bit is set, false otherwise
      • size

        public int size()
        Returns:
        The number of bits stored in this BitString
      • toString

        public String toString()
        Return a native String representation of the BitString.
        Overrides:
        toString in class Object
        Returns:
        A String representing the BitString