I have the following code that finds unique characters in a string using bit vectors. We assume it's an ASCII char set with lower case letters only.
I am having a hard time understanding the use of bit vectors below. Even after debugging through the program and following the changes variables go through after each loop.
// assuming that the characters range from a-z
static boolean isUniqueBitVector(String str) {
int checker = 0;
for(int i = 0; i < str.length(); i++) {
int val = str.charAt(i) - 'a';
if((checker & (1 << val)) > 0) {
return false;
} else {
checker |= (1 << val);
}
}
return true;
}
What's the purpose of left shifting the val(int representation of each char in string) by 1 and AND'ing it with checker (initialized to 0) and OR'ing it in the else block.