Check if a number has bits in an alternate pattern

We can quickly check if bits in a number are in an alternate pattern (like 101010). 

Compute bitwise XOR (XOR denoted using ^) of n and (n >> 1). If n has an alternate pattern, then n ^ (n >> 1) operation will produce a number having all bits set.

Below is the implementation of the above approach.

C++
// function to check if all the bits
// are set or not in the binary
// representation of 'n'
static bool allBitsAreSet(int n)
{
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return true;

    // else all bits are not set
    return false;
}

// Function to check if a number
// has bits in alternate pattern
bool bitsAreInAltOrder(unsigned int n)
{
    unsigned int num = n ^ (n >> 1);

    // To check if all bits are set in 'num'
    return allBitsAreSet(num);
}
Java
/*package whatever //do not write package name here */
import java.io.*;

class GFG {

  // function to check if all the bits
  // are set or not in the binary
  // representation of 'n'
  public static boolean allBitsAreSet(long n)
  {

    // if true, then all bits are set
    if (((n + 1) & n) == 0)
      return true;

    // else all bits are not set
    return false;
  }

  // Function to check if a number
  // has bits in alternate pattern
  public static boolean bitsAreInAltOrder(long n)
  {
    long num = n ^ (n >> 1);

    // To check if all bits are set in 'num'
    return allBitsAreSet(num);
  }
  public static void main (String[] args) {

  }
}

// This code is contributed by akashish__
Python
# function to check if all the bits
# are set or not in the binary
# representation of 'n'
def allBitsAreSet(n):
  # if true, then all bits are set
  if (((n + 1) & n) == 0):
    return True

  # else all bits are not set
  return False

# Function to check if a number
# has bits in alternate pattern
def bitsAreInAltOrder(n):
  num = n ^ (n >> 1)

  # To check if all bits are set in 'num'
  return allBitsAreSet(num)


# This code is contributed by akashish__
C#
using System;
public class GFG 
{

    // function to check if all the bits
    // are set or not in the binary
    // representation of 'n'
    public static bool allBitsAreSet(uint n)
    {
      
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return true;

        // else all bits are not set
        return false;
    }

    // Function to check if a number
    // has bits in alternate pattern
    public static bool bitsAreInAltOrder(uint n)
    {
        uint num = n ^ (n >> 1);

        // To check if all bits are set in 'num'
        return allBitsAreSet(num);
    }

    static public void Main() {}
}

// This code is contributed by akashish__
Javascript
// function to check if all the bits
// are set or not in the binary
// representation of 'n'
function allBitsAreSet(n)
{
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return true;

    // else all bits are not set
    return false;
}

// Function to check if a number
// has bits in alternate pattern
function bitsAreInAltOrder(n)
{
    let num = n ^ (n >> 1);

    // To check if all bits are set in 'num'
    return allBitsAreSet(num);
}

// This code is contributed by akashish__

Time Complexity: O(1)
Auxiliary Space: O(1)

DSA Self Paced Course




Bits manipulation (Important tactics)

Table of Contents

  • Compute XOR from 1 to n (direct method)
  • Count of numbers (x) smaller than or equal to n such that n+x = n^x
  • How to know if a number is a power of 2?
  • Find XOR of all subsets of a set
  • Find the number of leading, trailing zeroes and number of 1’s
  • Convert binary code directly into an integer in C++
  • The Quickest way to swap two numbers
  • Simple approach to flip the bits of a number
  • Finding the most significant set bit (MSB)
  • Check if a number has bits in an alternate pattern

Similar Reads

1. Compute XOR from 1 to n (direct method):

The  problem can be solved based on the following observations:...

2. Count of numbers (x) smaller than or equal to n such that n+x = n^x:

The count of such numbers x can be counted using the following mathematical trick....

3. How to know if a number is a power of 2?

This can be solved based on the following fact:...

4. Find XOR of all subsets of a set

We can do it in O(1) time. The answer is always 0 if the given set has more than one element. For sets with a single element, the answer is the value of the single element....

5. Find the number of leading, trailing zeroes and number of 1’s

We can quickly find the number of leading, trailing zeroes and number of 1’s in a binary code of an integer in C++ using GCC....

6. Convert binary code directly into an integer in C++

CPP // Conversion into Binary code #include using namespace std; int main() { auto number = 0b011; cout << number; return 0; } Java /*package whatever //do not write package name here */ // Conversion into Binary code import java.io.*; class GFG { public static void main(String[] args) { int number = 0b011; System.out.println(number); } } // This code is contributed by akashish__ Python # Python Code number = 0b011 print(number) # This code is contributed by akashish__ C# // Conversion into Binary code using System; public class GFG { static public void Main() { // Code int number = 0b011; Console.WriteLine(number); } } // This code is contributed by karthik Javascript // Conversion into Binary code let number = 0b011; console.log(number);...

7. The Quickest way to swap two numbers:

Two numbers can be swapped easily using the following bitwise operations:...

8. Finding the most significant set bit (MSB):

We can find the most significant set bit in O(1) time for a fixed size integer. For example below code is for 32-bit integer....

9. Check if a number has bits in an alternate pattern

We can quickly check if bits in a number are in an alternate pattern (like 101010)....