bit manipulation


  • 0
        public boolean hasAlternatingBits(int n) {
            
            int bit = n & 1;
            while (n != 0) {
                if (bit != (n & 1)) return false;
                bit ^= 1;
                n >>= 1;
            }
            return true;
        }
    

Log in to reply
 

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