Back to February Back to Year 2026
EASY 18 Feb 2026 View on LeetCode

693. Binary Number with Alternating Bits

</> Solution

/**
 * Problem: LeetCode 693 - Binary Number with Alternating Bits
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public boolean hasAlternatingBits(int n) {
        long x = n ^ (n >> 1);
        return (x & (x + 1)) == 0;
    }
}

TIME COMPLEXITY

O(1)

SPACE COMPLEXITY

O(1)

TOPICS

Bit Manipulation