Skip to content
Related Articles

Related Articles

Count numbers less than N whose Bitwise AND with N is zero

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 09 Aug, 2022
View Discussion
Improve Article
Save Article

Given a positive integer N, the task is to count all numbers which are less than N, whose Bitwise AND of all such numbers with N is zero.

Examples:

Input: N = 5
Output: 2
Explanation: The  integers less than N(= 5) whose Bitwise AND with 5 is 0 are 0 and 2. Hence, the total count is 2.

Input: N = 9
Output: 4

Naive approach: The idea is to go for every number less than n and check if it’s Bitwise AND with n is zero (0) or not. If bitwise AND becomes zero then increment the counter and finally return the counter.
Follow the steps below to implement the above idea:

  1. Initialize count with 0
  2. Iterate from i = 0 to n and calculate the bitwise AND of i with n
    If bitwise AND with n becomes zero then increment the value of count by 1.
  3. Return the count.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count numbers whose
// Bitwise AND with N equal to 0
int countBitwiseZero(int n)
{
    int count = 0;
    for (int i = 0; i < n; i++) {
 
        // If n&i == 0 then we will increase count by 1
        int temp = n & i;
        if (temp == 0) {
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    int n = 9;
    cout << countBitwiseZero(n);
    return 0;
}

Java




/*package whatever //do not write package name here */
import java.util.*;
 
class GFG {
    static int countBitwiseZero(int n){
      int count = 0;
    for (int i = 0; i < n; i++) {
 
        // If n&i == 0 then we will increase count by 1
        int temp = n & i;
        if (temp == 0) {
            count++;
        }
    }
    return count;
    }
  public static void main(String args[])
  {
    int n = 9;
    int ans = countBitwiseZero(n);
    System.out.print(ans);
  }
}
 
// This code is contributed by sayanc170.

Output

4

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

Approach: The given problem can be solved based on the observation that all bits which are set in N will be unset in any number which has Bitwise AND with N equal to 0. Follow the steps below to solve the problem:

  • Initialize a variable, say unsetBits, equal to the total number of unset bits in the given integer N.
  • Now, every unset bit in N can have either 0 or 1 in the corresponding position, as the Bitwise AND for any position where N has an unset bit will always be equal to 0. Hence, the total number of different possibilities will be 2 raised to the power of unsetBits.
  • Therefore, print the value of 2 to the power of unsetBits as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count number of
// unset bits in the integer N
int countUnsetBits(int N)
{
    // Stores the number of unset
    // bits in N
    int c = 0;
 
    while (N) {
 
        // Check if N is even
        if (N % 2 == 0) {
 
            // Increment the value of c
            c += 1;
        }
 
        // Right shift N by 1
        N = N >> 1;
    }
 
    // Return the value of
    // count of unset bits
    return c;
}
 
// Function to count numbers whose
// Bitwise AND with N equal to 0
void countBitwiseZero(int N)
{
    // Stores the number
    // of unset bits in N
    int unsetBits = countUnsetBits(N);
 
    // Print the value of 2 to the
    // power of unsetBits
    cout << (1 << unsetBits);
}
 
// Driver Code
int main()
{
    int N = 9;
    countBitwiseZero(N);
    return 0;
}

Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count number of
// unset bits in the integer N
static int countUnsetBits(int N)
{
    // Stores the number of unset
    // bits in N
    int c = 0;
 
    while (N != 0) {
 
        // Check if N is even
        if (N % 2 == 0) {
 
            // Increment the value of c
            c += 1;
        }
 
        // Right shift N by 1
        N = N >> 1;
    }
 
    // Return the value of
    // count of unset bits
    return c;
}
 
// Function to count numbers whose
// Bitwise AND with N equal to 0
static void countBitwiseZero(int N)
{
    // Stores the number
    // of unset bits in N
    int unsetBits = countUnsetBits(N);
 
    // Print the value of 2 to the
    // power of unsetBits
    System.out.print(1 << unsetBits);
}
 
// Driver Code
public static void main(String[] args)
{
     int N = 9;
    countBitwiseZero(N);
}
}
 
// This code is contributed by sanjoy_62.

Python3




# Python program for the above approach
 
# Function to count number of
# unset bits in the integer N
def countUnsetBits(N):
   
    # Stores the number of unset
    # bits in N
    c = 0
 
    while (N):
 
        # Check if N is even
        if (N % 2 == 0):
           
            # Increment the value of c
            c += 1
             
        # Right shift N by 1
        N = N >> 1
 
    # Return the value of
    # count of unset bits
    return c
 
# Function to count numbers whose
# Bitwise AND with N equal to 0
def countBitwiseZero(N):
   
    # Stores the number
    # of unset bits in N
    unsetBits = countUnsetBits(N)
 
    # Print value of 2 to the
    # power of unsetBits
    print ((1 << unsetBits))
 
# Driver Code
if __name__ == '__main__':
    N = 9
    countBitwiseZero(N)
 
    # This code is contributed by mohit kumar 29.

C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to count number of
// unset bits in the integer N
static int countUnsetBits(int N)
{
     
    // Stores the number of unset
    // bits in N
    int c = 0;
 
    while (N != 0)
    {
         
        // Check if N is even
        if (N % 2 == 0)
        {
             
            // Increment the value of c
            c += 1;
        }
 
        // Right shift N by 1
        N = N >> 1;
    }
 
    // Return the value of
    // count of unset bits
    return c;
}
 
// Function to count numbers whose
// Bitwise AND with N equal to 0
static void countBitwiseZero(int N)
{
     
    // Stores the number
    // of unset bits in N
    int unsetBits = countUnsetBits(N);
 
    // Print the value of 2 to the
    // power of unsetBits
    Console.Write(1 << unsetBits);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 9;
     
    countBitwiseZero(N);
}
}
 
// This code is contributed by shivanisinghss2110

Javascript




<script>
// Javascript program for the above approach
// Function to count number of
// unset bits in the integer N
function countUnsetBits( N)
{
    // Stores the number of unset
    // bits in N
    let c = 0;
 
    while (N != 0) {
 
        // Check if N is even
        if (N % 2 == 0) {
 
            // Increment the value of c
            c += 1;
        }
 
        // Right shift N by 1
        N = N >> 1;
    }
 
    // Return the value of
    // count of unset bits
    return c;
}
 
// Function to count numbers whose
// Bitwise AND with N equal to 0
function countBitwiseZero( N)
{
    // Stores the number
    // of unset bits in N
    let unsetBits = countUnsetBits(N);
 
    // Print the value of 2 to the
    // power of unsetBits
    document.write(1 << unsetBits);
}
 
// Driver Code
    let N = 9;
    countBitwiseZero(N);
 
// This code is contributed by shivansinghss2110
</script>

Output

4

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!