Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding a reader of bitvectors that will read a bitvector from a string

  • Loading branch information...
commit b79dc31420d349d7531582b4e8e2628ee9dbbe78 1 parent 589badd
@la3lma authored
Showing with 30 additions and 0 deletions.
  1. +30 −0 src/main/java/no/rmz/chordguesser/BitVector.java
View
30 src/main/java/no/rmz/chordguesser/BitVector.java
@@ -88,4 +88,34 @@ public boolean read(final int bit) {
int bi = bit % NO_OF_BITS_IN_A_BYTE;
return 0 != (bytes[by] & (1 << bi));
}
+
+ /**
+ * The bit-vector can be set to a bitpattern specified
+ * by a string, where the string "010" translates to
+ * a bitvector "010" where the leftmotst bit is the
+ * bit with index zero. The input string must contain
+ * only zeros and ones or an IllegalArgumentException will
+ * be thrown.
+ * @param bitString
+ */
+ public void setFromString(final String bitString) {
+ if (bitString.length() != lengthInBits) {
+ throw new IllegalArgumentException("The input string does not contain the correct number of characters, expected "
+ + lengthInBits + " but got " + bitString.length());
+ }
+
+ for (int i = 0, n = bitString.length(); i < n; i++) {
+ final char ch = bitString.charAt(i);
+
+ if (ch == '0') {
+ unset(i);
+ } else if (ch == '1') {
+ set(i);
+ } else {
+ throw new IllegalArgumentException("Unknon char in bit input string: " + ch);
+ }
+ }
+
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.