📜  计算与N的XOR等于与N的OR的数字

📅  最后修改于: 2021-04-29 05:14:57             🧑  作者: Mango

给定数字N,任务是找到X的计数,以使N XOR X == N OR X ,其中0 <= X <= N

例子:

方法:想法是将给定的数字转换为二进制,然后计算其中的未设置位。 2 ^ count给我们X的数量,使得N XOR X == N OR X。

下面是上述方法的实现:

C++
// C++ program to find 
// the XOR equals OR count 
#include
#include
using namespace std; 
  
class gfg { 
      
    // Function to calculate count 
    // of numbers with XOR equals OR 
    public:
    int xorEqualsOrCount(int N) 
    { 
          
        // variable to store count of unset bits 
        int count = 0; 
        int bit; 
        while (N > 0) { 
              
            bit = N % 2; 
            if (bit == 0) 
                count++; 
            N = N / 2; 
        } 
        return (int)pow(2, count); 
    } };
  
    // Driver code 
    int main() 
    { 
        gfg g ;
        int N = 7; 
        cout<


Java
// Java program to find the XOR equals OR count
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Function to calculate count of numbers with XOR equals OR
    static int xorEqualsOrCount(int N)
    {
        // variable to store count of unset bits
        int count = 0;
        int bit;
        while (N > 0) {
            bit = N % 2;
            if (bit == 0)
                count++;
            N = N / 2;
        }
        return (int)Math.pow(2, count);
    }
  
    // Driver code
    public static void main(String args[])
    {
        int N = 7;
        System.out.println(xorEqualsOrCount(N));
    }
}


Python3
# Python3 program to find
# the XOR equals OR count
  
# Function to calculate count
# of numbers with XOR equals OR
def xorEqualsOrCount(N) :
  
    # variable to store
    # count of unset bits
    count = 0
  
    while(N > 0) :
  
        bit = N % 2
  
        if bit == 0 :
            count += 1
  
        N //= 2
  
    return int(pow(2, count))
  
# Driver code     
if __name__ == "__main__" :
  
    N = 7
    print(xorEqualsOrCount(N))
                  
# This code is contributed by 
# ANKITRAI1


C#
// C# program to find 
// the XOR equals OR count
using System;
  
class GFG {
  
    // Function to calculate count 
    // of numbers with XOR equals OR
    static int xorEqualsOrCount(int N)
    {
          
        // variable to store count of unset bits
        int count = 0;
        int bit;
        while (N > 0) {
              
            bit = N % 2;
            if (bit == 0)
                count++;
            N = N / 2;
        }
        return (int)Math.Pow(2, count);
    }
  
    // Driver code
    public static void Main()
    {
        int N = 7;
        Console.WriteLine(xorEqualsOrCount(N));
    }
}
  
// This code is contributed by inder_verma..


PHP
 0) 
    { 
        $bit = $N % 2; 
        if ($bit == 0) 
            $count++; 
        $N = intval($N / 2); 
    } 
    return pow(2, $count); 
} 
  
// Driver code 
$N = 7; 
echo xorEqualsOrCount($N);
  
// This code is contributed
// by ChitraNayal
?>


输出:
1