📌  相关文章
📜  计数小于 N 且与 N 的按位 AND 为零的数字

📅  最后修改于: 2021-10-26 06:52:29             🧑  作者: Mango

给定一个正整数N ,任务是计算所有小于N 的数字,并且所有这些数字与N 的按位 AND 为零。

例子:

方法:给定的问题可以根据观察来解决,即在N中设置的所有位将在任何具有按位与N等于0 的数字中取消设置。请按照以下步骤解决问题:

  • 初始化一个变量,比如unsetBits ,等于给定整数N中未设置位的总数。
  • 现在, N 中的每个未设置位在相应位置都可以为01 ,因为对于N具有未设置位的任何位置的按位 AND 将始终等于0 。因此,不同可能性的总数将是2unsetBits 次方
  • 因此,将 2 的值打印为unsetBits 的幂作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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)
 
    # Prthe 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


输出:
4

时间复杂度: O(log N)
辅助空间: O(1)