📌  相关文章
📜  与 N 的按位异或大于 N 的 X 的所有可能值的计数

📅  最后修改于: 2022-05-13 01:56:05.645000             🧑  作者: Mango

与 N 的按位异或大于 N 的 X 的所有可能值的计数

给定一个整数N计算X的值的数量,使得XN > N 其中表示按位异或运算

例子:

朴素方法:解决此问题的简单方法是从1 迭代到 N并在X XOR N >N for 0 < X < N时增加计数。最后,返回值的数量。

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

有效方法:最有效的方法是找到最接近 2 的下一个幂的数字,该数字已设置所有位,最后从原始数字中减去它。下面给出了解决方案的数学观察:

请按照以下步骤解决问题:

  • 求 2 的最接近的下一个幂。
  • 从最接近的 2 的幂中减去 1,以便给定数字的所有位都已设置
  • 最后,从原始数字中减去并返回它

下面是上述方法的实现:

C++
// C++ program for find the count of
// number of value of X such that  N XOR X >N
 
#include 
using namespace std;
 
// Function for  calculating the total
// possible value satisfy the condition
// X⊕N > N and 0 < X < N
void maximumXor(long long N)
{
    long long num = N;
    long long int count;
 
    count = 0;
    while (N > 0) {
 
        // Count the total number of bits
        count++;
        N = N / 2;
    }
    long long next_power = ((long long)1 << count);
 
    // Finding the next power of 2
    // of the given number
    long long result = next_power - 1;
 
    // Finding the number nearest to
    // the nextpower of 2 which has all
    // its bits set
    cout << result - num;
}
 
// Driver Code
int main()
{
    int N = 10;
    maximumXor(N);
    return 0;
}


Java
// Java program for find the count of
// number of value of X such that  N XOR X >N
import java.util.*;
public class GFG {
 
  // Function for  calculating the total
  // possible value satisfy the condition
  // X⊕N > N and 0 < X < N
  static void maximumXor(long N)
  {
    long num = N;
    long count = 0;
 
    count = 0;
    while (N > 0) {
 
      // Count the total number of bits
      count++;
      N = N / 2;
    }
    long next_power = ((long)1 << count);
 
    // Finding the next power of 2
    // of the given number
    long result = next_power - 1;
 
    // Finding the number nearest to
    // the nextpower of 2 which has all
    // its bits set
    System.out.print(result - num);
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 10;
    maximumXor(N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# python3 program for find the count of
# number of value of X such that N XOR X >N
 
# Function for calculating the total
# possible value satisfy the condition
# X⊕N > N and 0 < X < N
def maximumXor(N):
 
    num = N
    count = 0
 
    while (N > 0):
 
        # Count the total number of bits
        count += 1
        N = N // 2
 
    next_power = (1 << count)
 
    # Finding the next power of 2
    # of the given number
    result = next_power - 1
 
    # Finding the number nearest to
    # the nextpower of 2 which has all
    # its bits set
    print(result - num)
 
# Driver Code
if __name__ == "__main__":
 
    N = 10
    maximumXor(N)
 
# This code is contributed by rakeshsahni


C#
// C# program for find the count of
// number of value of X such that  N XOR X >N
using System;
 
public class GFG
{
 
  // Function for  calculating the total
  // possible value satisfy the condition
  // X⊕N > N and 0 < X < N
  static void maximumXor(long N)
  {
    long num = N;
    long count = 0;
 
    count = 0;
    while (N > 0) {
 
      // Count the total number of bits
      count++;
      N = N / 2;
    }
    long next_power = (1 << (int)count);
 
    // Finding the next power of 2
    // of the given number
    long result = next_power - 1;
 
    // Finding the number nearest to
    // the nextpower of 2 which has all
    // its bits set
    Console.Write(result - num);
  }
 
  // Driver Code
  public static void Main(String []args)
  {
    int N = 10;
    maximumXor(N);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript



输出
5

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