To Understand how it is working in bit level put Integer.toBinaryString() and check. Basically whenever you use bit wise operator in any integral value actual operation happen on there respective binary values.
public static boolean isUniqueChars(String str) {
if (str.length() > 128) {
return false;
} int checker = 0;
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i) - 'a';
System.out.println("Actual Value " + val); // ASCII substraction
System.out.println("Actual Value in Binary " + Integer.toBinaryString(val));
System.out.println("Left Shift Value in Binary " + Integer.toBinaryString(1<<val));
System.out.println("Operator Value is" + Integer.toBinaryString(checker & (1 << val))));
if ((checker & (1 << val)) > 0)
return false;
checker |= (1 << val); // checker = checker | (1<<val)
System.out.println("Checker Value in Binary " + Integer.toBinaryString(checker));
}
return true;
}
Run this and and check those print statement then you can identify how bit wise operator is working.
Logically:
1 is every time left shifted by ASCII substracted value and then BIT(or) is done with previous checker value. If current and checker value BIT(or) returns > 0 that means some bit position of both the value has 1. and thus its a repeating char. I know it sounds little complicate but take your time and debug it.
hope it helps you.