Java boolean operator &=, |= and ^=

a &= b also means a = a & b
 
true    &    true    ==>    true
true    &    false    ==>    false
false   &    true    ==>    false
false   &    false    ==>    false
 
true    |    true    ==>    true
true    |    false    ==>    true
false   |    true    ==>    true
false   |    false    ==>    false
 
^=  相同为假,不同为真
true    ^    true    ==>    false
true    ^    false    ==>    true
false   ^    true    ==>    true
false   ^    false    ==>    false
 
public class UniqueChar {
    public static boolean isUniqueChars(String str) {
        int checker = 0; //bit storage
        for (int i = 0; i < str.length(); ++i) {
            int val = str.charAt(i) - 'a';
            // if bit at index val is 1, then it already exists
            if ((checker & (1 << val)) > 0) {
                return false;
            }

            //Set bit of index val to 1
            checker |= (1 << val);
        }
        return true;
    }
}

 

posted @ 2018-01-23 06:56  小张的练习室  阅读(373)  评论(0编辑  收藏  举报