Check if a number has bits in alternate pattern

Last Updated : 25 Jul, 2026

Given a non negative integer n, check whether its binary representation consists of alternating bits. Return true if every adjacent pair of bits is different otherwise, return false.

Examples : 

Input: n = 12
Output: false
Explanation: n = 12 = "1100". Hence there is no alternate pattern.

Input: n = 10
Output: true
Explanation: n = 10 = "1010". Hence n has an alternate pattern.

Try It Yourself
redirect icon

[Naive Approach] Bitwise Adjacent Check - O(log n) Time and O(1) Space

Extract bits from right to left using bitwise operations. Check if adjacent bits are same. If any equal adjacent bits found, return false.

  • If n is zero, return true
  • Get last bit as prevBit
  • Right shift n by 1
  • While n > 0, extract current bit
  • If current bit equals prevBit, return false
  • Update prevBit and right shift n
  • Return true
C++
#include <iostream>
using namespace std;

bool alternateBits(int n)
{
    if (n == 0)
        return true;

    int prevBit = n & 1;
    n >>= 1;

    while (n > 0)
    {
        int currBit = n & 1;

        // If adjacent bits are same, not alternating
        if (currBit == prevBit)
        {
            return false;
        }

        prevBit = currBit;
        n >>= 1;
    }

    return true;
}

int main()
{
    int n = 5;
    cout << (alternateBits(n) ? "true" : "false") << endl;
    return 0;
}
Java
import java.util.*;

class GfG {
    
    static boolean alternateBits(int n) {
        if (n == 0)
            return true;
        
        int prevBit = n & 1;
        n >>= 1;
        
        while (n > 0) {
            int currBit = n & 1;
            
            // If adjacent bits are same, not alternating
            if (currBit == prevBit) {
                return false;
            }
            
            prevBit = currBit;
            n >>= 1;
        }
        
        return true;
    }
    
    public static void main(String[] args) {
        int n = 5;
        System.out.println(alternateBits(n) ? "true" : "false");
    }
}
Python
def alternateBits(n):
    if n == 0:
        return True
    
    prevBit = n & 1
    n >>= 1
    
    while n > 0:
        currBit = n & 1
        
        # If adjacent bits are same, not alternating
        if currBit == prevBit:
            return False
        
        prevBit = currBit
        n >>= 1
    
    return True

if __name__ == "__main__":
    n = 5
    print("true" if alternateBits(n) else "false")
C#
using System;

class GfG {
    
    static bool alternateBits(int n) {
        if (n == 0)
            return true;
        
        int prevBit = n & 1;
        n >>= 1;
        
        while (n > 0) {
            int currBit = n & 1;
            
            // If adjacent bits are same, not alternating
            if (currBit == prevBit) {
                return false;
            }
            
            prevBit = currBit;
            n >>= 1;
        }
        
        return true;
    }
    
    static void Main(string[] args) {
        int n = 5;
        Console.WriteLine(alternateBits(n) ? "true" : "false");
    }
}
JavaScript
function alternateBits(n) {
    if (n === 0)
        return true;
    
    let prevBit = n & 1;
    n >>= 1;
    
    while (n > 0) {
        let currBit = n & 1;
        
        // If adjacent bits are same, not alternating
        if (currBit === prevBit) {
            return false;
        }
        
        prevBit = currBit;
        n >>= 1;
    }
    
    return true;
}

// Driver code
const n = 5;
console.log(alternateBits(n) ? "true" : "false");

Output
true

[Expected Approach] XOR Property - O(1) Time and O(1) Space

If binary representation has alternating bits, then n XOR (n >> 1) will have all bits set to 1. Check if x has all bits set using (x & (x+1)) == 0.

  • Compute x = n ^ (n >> 1)
  • If x has all bits set, x & (x+1) will be 0
  • Return true if (x & (x+1)) == 0
  • Else return false
C++
#include <iostream>
using namespace std;

bool alternateBits(int n)
{
    // If bits are alternating, n ^ (n >> 1) will have all bits set to 1
    int x = n ^ (n >> 1);

    // Check if x has all bits set (i.e., x+1 is power of 2)
    return (x & (x + 1)) == 0;
}

int main()
{
    int n = 5;
    cout << (alternateBits(n) ? "true" : "false") << endl;
    return 0;
}
Java
import java.util.*;

class GfG {
    
    static boolean alternateBits(int n) {
        // If bits are alternating, n ^ (n >> 1) will have all bits set to 1
        int x = n ^ (n >> 1);
        
        // Check if x has all bits set (i.e., x+1 is power of 2)
        return (x & (x + 1)) == 0;
    }
    
    public static void main(String[] args) {
        int n = 5;
        System.out.println(alternateBits(n) ? "true" : "false");
    }
}
Python
def alternateBits(n):
    
    # If bits are alternating, n ^ (n >> 1) will have all bits set to 1
    x = n ^ (n >> 1)
    
    # Check if x has all bits set (i.e., x+1 is power of 2)
    return (x & (x + 1)) == 0

if __name__ == "__main__":
    n = 5
    print("true" if alternateBits(n) else "false")
C#
using System;

class GfG {
    
    static bool alternateBits(int n) {
        // If bits are alternating, n ^ (n >> 1) will have all bits set to 1
        int x = n ^ (n >> 1);
        
        // Check if x has all bits set (i.e., x+1 is power of 2)
        return (x & (x + 1)) == 0;
    }
    
    static void Main(string[] args) {
        int n = 5;
        Console.WriteLine(alternateBits(n) ? "true" : "false");
    }
}
JavaScript
function alternateBits(n) {
    // If bits are alternating, n ^ (n >> 1) will have all bits set to 1
    let x = n ^ (n >> 1);
    
    // Check if x has all bits set (i.e., x+1 is power of 2)
    return (x & (x + 1)) === 0;
}

// Driver code
const n = 5;
console.log(alternateBits(n) ? "true" : "false");

Output
true
Comment