JAVA clean bit manipulation


  • 0
    H
        public boolean hasAlternatingBits(int n) {
            int pre;
            pre = n & 1;
            n = n >> 1;
            while (n != 0) {
                int cur = n & 1;
                if (pre + cur != 1) return false;
                pre = cur;
                n = n >> 1;
            }
            return true;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.