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

📅  最后修改于: 2021-06-25 13:30:10             🧑  作者: Mango

给定数字N。任务是计算x的所有可能值,以使n \oplus  x等于(Nx),其中\oplus  表示按位XOR运算。
例子:

Input: N = 3
Output: 4
The all possible values of x are respectively 0, 1, 2, 3.

Input: N = 6
Output: 4
The all possible values of x are respectively 0, 2, 4, 6.

方法:如果两个位的符号相反,则两个位的XOR值分别为1;如果两个位的符号相同,则XOR值为0。因此,根据XOR的性质,我们可以说n \oplus  x始终大于或等于nx。其值等于nx的唯一条件是x的位形成n的位的子集。因为如果在第i个位置xn都设置了位,则xor之后的值将减小,减小的值将为2^{i}  ,其中i是从0开始的位置。
所以答案是第n个位的子集的总数为2^{k}  ,其中k是n中设置位的计数。
下面是上述方法的实现:

C++
#include 
using namespace std;
 
// function to Count all values of x
void count_values(int n)
{
    // Count set bits in n
    // by using stl function
    int set_bits = __builtin_popcount(n);
 
    // count all subset of set bits
    cout << pow(2, set_bits) << "\n";
}
 
// Driver code
int main()
{
 
    int n = 27;
    count_values(n);
 
    return 0;
}


Java
import java.util.*;
 
class Solution
{
//count number of set bits
static int __builtin_popcount(int n)
{
    //count variable
    int count=0;
     
    while(n>0)
    {
        //if the bit is 1
        if(n%2==1)
        count++;
         
        n=n/2;
    }
    return count;
}
     
// function to Count all values of x
static void count_values(int n)
{
    // Count set bits in n
    // by using stl function
    int set_bits = __builtin_popcount(n);
   
    // count all subset of set bits
    System.out.println((int)Math.pow(2, set_bits));
}
   
// Driver code
public static void main(String args[])
{
   
    int n = 27;
    count_values(n);
   
 
}
}
 
// This code is contributed
// by Arnab Kundu


Python 3
# Python3 program to implement
# above approach
 
# from math import pow method
from math import pow
 
# count number of set bits
def __builtin_popcount(n) :
 
    # count variable
    count = 0
 
    while n > 0 :
 
        # if the bit is 1
        if n % 2 == 1 :
            count += 1
 
        n = n//2
         
    return count
 
 
# function to Count all values of x
def count_values(n) :
 
    set_bits = __builtin_popcount(n)
 
    # count all subset of set bits
    print(int(pow(2, set_bits)))
 
 
# Driver code
if __name__ == "__main__" :
 
    n = 27
    count_values(n)
 
# This code is contributed by
# ANKITRAI1


C#
using System;
class GFG
{
// count number of set bits
static int __builtin_popcount(int n)
{
    // count variable
    int count = 0;
     
    while(n > 0)
    {
        //if the bit is 1
        if(n % 2 == 1)
        count++;
         
        n = n / 2;
    }
    return count;
}
     
// function to Count all values of x
static void count_values(int n)
{
    // Count set bits in n
    // by using stl function
    int set_bits = __builtin_popcount(n);
     
    // count all subset of set bits
    Console.Write((int)Math.Pow(2, set_bits));
}
     
// Driver code
public static void Main()
{
    int n = 27;
    count_values(n);
}
}
 
// This code is contributed by Smitha


PHP
 0)
    {
        //if the bit is 1
        if($n % 2 == 1)
            $count++;
         
        $n = $n / 2;
    }
    return $count;
}
     
// function to Count all values of x
function count_values($n)
{
    // Count set bits in n
    // by using stl function
    $set_bits = __builtin_popcount($n);
 
    // count all subset of set bits
    echo (int)pow(2, $set_bits);
}
 
// Driver code
$n = 27;
count_values($n);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>


Javascript


输出:
16

时间复杂度: O(k),其中k是N中的设置位数。