Class BitString

java.lang.Object
org.apache.directory.api.asn1.util.BitString

public class BitString extends Object
Implement the Bit String primitive type. A BitString is internally stored as an array of byte.
Author:
Apache Directory Project
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final BitString
    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).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    clearBit(int pos)
    Clear a bit at a specified position.
    boolean
    getBit(int pos)
    Get the bit stored into the BitString at a specific position.
    byte[]
    Get the representation of a BitString.
    byte
    Get the number of unused bits
    void
    setBit(int pos)
    Set a bit at a specified position.
    void
    setData(byte[] data)
    Set a new BitString in the BitString.
    int
    Get the number of stored bits
    Return a native String representation of the BitString.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • EMPTY_STRING

      public static final BitString EMPTY_STRING
      A null MutableString
  • Constructor Details

    • 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 Details

    • 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()
      Get the number of stored bits
      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