/**
* Immutable class representing a {@link Bit}Vector
*
- *
* @author Christian Femers
*
*/
private BitVector(Bit[] bits)
{
- this.bits = Objects.requireNonNull(bits);// do this first to "catch" bits==null before the foreach loop
+ this.bits = Objects.requireNonNull(bits); // do this first to "catch" bits==null before the foreach loop
for (Bit bit : bits)
if (bit == null)
throw new NullPointerException();
return BitVectorMutator.of(this);
}
- public Bit getBit(int bitIndex)
+ /**
+ * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public Bit getMSBit(int bitIndex)
{
return bits[bitIndex];
}
+ /**
+ * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public Bit getLSBit(int bitIndex)
+ {
+ return bits[bits.length - bitIndex - 1];
+ }
+
public Bit[] getBits()
{
return bits.clone();
return this;
}
- public void setBit(int bitIndex, Bit bit)
+ /**
+ * Set the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public void setMSBit(int bitIndex, Bit bit)
{
bits[bitIndex] = bit;
}
- public Bit getBit(int bitIndex)
+ /**
+ * Set the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public void setLSBit(int bitIndex, Bit bit)
+ {
+ bits[bits.length - bitIndex - 1] = bit;
+ }
+
+ /**
+ * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public Bit getMSBit(int bitIndex)
{
return bits[bitIndex];
}
+ /**
+ * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public Bit getLSBit(int bitIndex)
+ {
+ return bits[bits.length - bitIndex - 1];
+ }
+
public int length()
{
return bits.length;
return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
}
- @Override
- public String toString()
- {
- return toBitStringMSBFirst();
- }
-
- /**
- * All {@link Bit}s symbols concatenated together
- *
- * @see #parse(String)
- */
- public String toBitStringLSBFirst()
- {
- StringBuilder sb = new StringBuilder(bits.length);
- for (Bit bit : bits)
- sb.append(bit);
- return sb.toString();
- }
-
/**
- * All {@link Bit}s symbols concatenated together, with the MSB coming first (like a binary number)
+ * All {@link Bit}s symbols concatenated together (MSB first)
*
* @see #parse(String)
*/
- public String toBitStringMSBFirst()
+ @Override
+ public String toString()
{
StringBuilder sb = new StringBuilder(bits.length);
for (Bit bit : bits)
sb.append(bit);
- sb.reverse();
return sb.toString();
}
/**
- * Parses a String containing solely {@link Bit} symbols
+ * Parses a String containing solely {@link Bit} symbols (MSB first)
*
- * @see #toBitStringLSBFirst()
+ * @see #toString()
*/
- public static BitVector parseLSBFirst(String s)
+ public static BitVector parse(String s)
{
Bit[] values = new Bit[s.length()];
for (int i = 0; i < s.length(); i++)
}
/**
- * Parses a String containing solely {@link Bit} symbols, with the MSB coming first (like a binary number)
- *
- * @see #toBitStringLSBFirst()
+ * Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
*/
- public static BitVector parseMSBFirst(String s)
- {
- Bit[] values = new Bit[s.length()];
- for (int i = 0, j = s.length() - 1; j >= 0; i++, j--)
- {
- values[i] = Bit.parse(s, j);
- }
- return new BitVector(values);
- }
-
@Override
public Iterator<Bit> iterator()
{
{
if (!hasNext())
throw new NoSuchElementException();
- return getBit(pos++);
+ return getMSBit(pos++);
}
@Override