Skip to content

Commit

Permalink
Adding a reader of bitvectors that will read a bitvector from a string
Browse files Browse the repository at this point in the history
  • Loading branch information
la3lma committed May 5, 2012
1 parent 589badd commit b79dc31
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/no/rmz/chordguesser/BitVector.java
Expand Up @@ -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);
}
}


}
}

0 comments on commit b79dc31

Please sign in to comment.