I have a program which I found online which basically tells whether the String
contains all unique characters, below is the code
private static boolean areCharsUnique(String str) {
if (str.length() > 256)
return false;
int checker = 0;
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i) - 'a';
if ((checker & (1 << val)) > 0) {
return false;
}
checker |= (1 << val);
}
return true;
}
I am baffled by this line of code if ((checker & (1 << val)) > 0)
and also
checker |= (1 << val);
I know that <<
is a left shift operator, but how exactly left shifting helps in the above situation?
In short how does the above program work?