Count numbers less than N whose Bitwise AND with N is zero
Last Updated :
06 Feb, 2023
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:
- Initialize count with 0
- 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. - 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.
Python3
# Python3 code to implement the above approach
def countBitwiseZero(n):
count = 0
for i in range(n):
temp = n & i
# Checking if bitwise and is 0
if temp == 0:
# updating count
count += 1
return count
# Driver Code
n = 9
# Function call
print(countBitwiseZero(n))
# This code is contributed by phasing17.
C#
// C# code to implement the approach
using System;
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;
}
// Driver code
static void Main(string[] args)
{
int n = 9;
// Function call
int ans = CountBitwiseZero(n);
Console.WriteLine(ans);
}
}
// This code is contributed by phasing17
JavaScript
// JavaScript code to implement the above approach
function countBitwiseZero(n) {
let count = 0;
for (let i = 0; i < n; i++) {
let temp = n & i;
if (temp == 0) {
count++;
}
}
return count;
}
// Driver Code
let n = 9;
console.log(countBitwiseZero(n));
//This code is contributed by phasing17.
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>
Time Complexity: O(log N)
Auxiliary Space: O(1)
Similar Reads
Count of N-bit binary numbers without leading zeros Given an integer N, the task is to find the count of N-bit binary numbers without leading zeros.Examples: Input: N = 2 Output: 2 10 and 11 are the only possible binary numbers.Input: N = 4 Output: 8 Approach: Since the numbers cannot have leading zeros so the left-most bit has to be set to 1. Now fo
2 min read
Numbers whose bitwise OR and sum with N are equal Given a non-negative integer N, the task is to find count of non-negative integers less than or equal to N whose bitwise OR and sum with N are equal. Examples : Input : N = 3 Output : 1 0 is the only number in [0, 3] that satisfies given property. (0 + 3) = (0 | 3) Input : 10 Output : 4 (0 + 10) = (
6 min read
Count integers less than A so that Bitwise AND of B with XOR of A and the number is 0 Given two positive integers A and B, the task is to find the number of integers X such that (AâX ) & B = 0 and 0 ⤠X < A where â is bitwise XOR and & is bitwise AND operator. Examples: Input: A = 3, B = 5Output: 1â¨Explanation: Here A = 3, B = 5. So we need to find the number of X, which s
5 min read
Count triples with Bitwise AND equal to Zero Given an array of integers A[] consisting of N integers, find the number of triples of indices (i, j, k) such that A[i] & A[j] & A[k] is 0(<0 ? i, j, k ? N and & denotes Bitwise AND operator. Examples: Input: A[]={2, 1, 3}Output: 12Explanation: The following i, j, k triples can be cho
7 min read
Count values whose Bitwise OR with A is equal to B Given two integers A and B, the task is to count possible values of X that satisfies the condition A | X = B. Note: | represents Bitwise OR operation. Examples: Input: A = 2, B = 3Output: 2Explanation: Since, 2 | 1 = 3 and 2 | 3 = 3. Therefore, the possible values of x are 1 and 3. Input: A = 5, B =
6 min read
Count of N-digit numbers in base K with no two consecutive zeroes Given two integers N and K, the task is to find the count of all the integer in base K which satisfy the following conditions: The integers must be of exactly N digits.There should be no leading 0.There must not be any consecutive digit pair such that both the digits are 0.Examples: Input: N = 3, K
15+ min read