📜  计算与x的XOR大于x的较小值

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

给定整数“ x”,找到满足以下条件的“ a”值的数量:

  1. XOR x> x
  2. 0

例子 :

Input : x = 10 
Output : 5
Explanation: For x = 10, following 5 values
             of 'a' satisfy the conditions:
             1 XOR 10 = 11
             4 XOR 10 = 14
             5 XOR 10 = 15
             6 XOR 10 = 12
             7 XOR 10 = 13 

Input : x = 2
Output : 1
Explanation: For x=2, we have just one value
             1 XOR 2 = 3.

天真的方法
一种简单的方法是检查“ a”在0到“ x”之间的所有值,并使用x计算其XOR
并检查条件1是否满足。

C++
// C++ program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
#include
using namespace std;
  
int countValues(int x)
{
    int count = 0;
    for (int i=1; i < x; i++)
        if ((i ^ x) > x)
            count++;
    return count;
}
  
// Driver code
int main()
{
    int x = 10;
    cout << countValues(x);
    return 0;
}


Java
// Java program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
  
public class XOR
{
    static int countValues(int x)
    {
        int count = 0;
        for (int i=1; i < x; i++)
            if ((i ^ x) > x)
                count++;
        return count;
    }
      
    public static void main (String[] args)
    {
        int x = 10;
        System.out.println(countValues(x));
    }
}
  
// This code is contributed by Saket Kumar


Python 3
# Python3 program to find 
# count of values whose
# XOR with x is greater
# than x and values are 
# smaller than x
  
def countValues(x):
  
    count = 0
    for i in range(1 ,x):
        if ((i ^ x) > x):
            count += 1
    return count
  
# Driver code
x = 10
print(countValues(x))
  
# This code is contributed 
# by Smitha


C#
// C# program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
using System;
  
class GFG
{
    static int countValues(int x)
    {
        int count = 0;
        for (int i = 1; i < x; i++)
            if ((i ^ x) > x)
                count++;
        return count;
    }
      
    public static void Main ()
    {
        int x = 10;
        Console.Write(countValues(x));
    }
}
  
// This code is contributed by nitin mittal.


PHP
 $x)
            $count++;
    return $count;
}
  
    // Driver code
    $x = 10;
    echo countValues($x);
  
// This code is contributed by anuj_67.
?>


C++
// C++ program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
#include
using namespace std;
  
int countValues(int x)
{
    // Initialize result
    int count = 0, n = 1;
  
    // Traversing through all bits of x
    while (x != 0)
    {
        // If current last bit of x is set
        // then increment count by n. Here
        // n is a power of 2 corresponding
        // to position of bit
        if (x%2 == 0)
            count += n;
  
        // Simultaneously calculate the 2^n
        n *= 2;
  
        // Replace x with x/2;
        x /= 2;
    }
  
    return count;
}
  
// Driver code
int main()
{
    int x = 10;
    cout << countValues(x);
    return 0;
}


Java
// Java program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
  
class GFG
{
    static int countValues(int x)
    {
        // Initialize result
        int count = 0, n = 1;
          
        // Traversing through all bits of x
        while (x != 0)
        {
            // If current last bit of x is set
            // then increment count by n. Here
            // n is a power of 2 corresponding
            // to position of bit
            if (x % 2 == 0)
                count += n;
                  
            // Simultaneously calculate the 2^n
            n *= 2;
              
            // Replace x with x/2;
            x /= 2;
        }
        return count;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int x = 10;
        System.out.println(countValues(x));
    }
      
}
  
// This code is contributed by Saket Kumar


Python3
# Python3 program to find count
# of values whose XOR with
# x is greater than x and 
# values are smaller than x
  
def countValues(x):
      
    # Initialize result
    count = 0;
    n = 1;
  
    # Traversing through 
    # all bits of x
    while (x > 0):
          
        # If current last bit 
        # of x is set then
        # increment count by 
        # n. Here n is a power
        # of 2 corresponding
        # to position of bit
        if (x % 2 == 0):
            count += n;
  
        # Simultaneously 
        # calculate the 2^n
        n *= 2;
  
        # Replace x with x/2;
        x /= 2;
        x = int(x);
  
    return count;
  
# Driver code
x = 10;
print(countValues(x));
  
# This code is contributed
# by mits


C#
// C# program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
using System;
  
class GFG
{
    static int countValues(int x)
    {
        // Initialize result
        int count = 0, n = 1;
          
        // Traversing through all bits of x
        while (x != 0)
        {
            // If current last bit of x is set
            // then increment count by n. Here
            // n is a power of 2 corresponding
            // to position of bit
            if (x % 2 == 0)
                count += n;
                  
            // Simultaneously calculate the 2^n
            n *= 2;
              
            // Replace x with x/2;
            x /= 2;
        }
        return count;
    }
      
    // Driver code
    public static void Main ()
    {
        int x = 10;
        Console.Write(countValues(x));
    }
      
}
  
// This code is contributed by nitin mittal


PHP


输出 :

5

上述方法的时间复杂度为O(x)。

高效方法
有效的解决方案在于数字的二进制表示形式。我们以二进制表示考虑全0。对于第i个位置上的每个0,我们可以让2个小于或等于x的i数具有更大的XOR。

C++

// C++ program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
#include
using namespace std;
  
int countValues(int x)
{
    // Initialize result
    int count = 0, n = 1;
  
    // Traversing through all bits of x
    while (x != 0)
    {
        // If current last bit of x is set
        // then increment count by n. Here
        // n is a power of 2 corresponding
        // to position of bit
        if (x%2 == 0)
            count += n;
  
        // Simultaneously calculate the 2^n
        n *= 2;
  
        // Replace x with x/2;
        x /= 2;
    }
  
    return count;
}
  
// Driver code
int main()
{
    int x = 10;
    cout << countValues(x);
    return 0;
}

Java

// Java program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
  
class GFG
{
    static int countValues(int x)
    {
        // Initialize result
        int count = 0, n = 1;
          
        // Traversing through all bits of x
        while (x != 0)
        {
            // If current last bit of x is set
            // then increment count by n. Here
            // n is a power of 2 corresponding
            // to position of bit
            if (x % 2 == 0)
                count += n;
                  
            // Simultaneously calculate the 2^n
            n *= 2;
              
            // Replace x with x/2;
            x /= 2;
        }
        return count;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int x = 10;
        System.out.println(countValues(x));
    }
      
}
  
// This code is contributed by Saket Kumar

Python3

# Python3 program to find count
# of values whose XOR with
# x is greater than x and 
# values are smaller than x
  
def countValues(x):
      
    # Initialize result
    count = 0;
    n = 1;
  
    # Traversing through 
    # all bits of x
    while (x > 0):
          
        # If current last bit 
        # of x is set then
        # increment count by 
        # n. Here n is a power
        # of 2 corresponding
        # to position of bit
        if (x % 2 == 0):
            count += n;
  
        # Simultaneously 
        # calculate the 2^n
        n *= 2;
  
        # Replace x with x/2;
        x /= 2;
        x = int(x);
  
    return count;
  
# Driver code
x = 10;
print(countValues(x));
  
# This code is contributed
# by mits

C#

// C# program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
using System;
  
class GFG
{
    static int countValues(int x)
    {
        // Initialize result
        int count = 0, n = 1;
          
        // Traversing through all bits of x
        while (x != 0)
        {
            // If current last bit of x is set
            // then increment count by n. Here
            // n is a power of 2 corresponding
            // to position of bit
            if (x % 2 == 0)
                count += n;
                  
            // Simultaneously calculate the 2^n
            n *= 2;
              
            // Replace x with x/2;
            x /= 2;
        }
        return count;
    }
      
    // Driver code
    public static void Main ()
    {
        int x = 10;
        Console.Write(countValues(x));
    }
      
}
  
// This code is contributed by nitin mittal

的PHP



输出 :

5

该解决方案的时间复杂度为O(Log x)