zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    uva 1637 Double Patience
    Emacs ^ Vim
    uva 11181 Probability|Given
    uva 10491 Cows and Cars
    uva 1636 Headshot
    hdu 4336 Card Collector
    zoj 3640 Help Me Escape
    Codeforces 148 D Bag of mice
    hdu 4405 Aeroplane chess
    hdu 3853 LOOPS
  • 原文地址:https://www.cnblogs.com/codingforum/p/8333799.html
Copyright © 2011-2022 走看看