📜  计算与n的XOR值较大的较小数字

📅  最后修改于: 2021-04-23 21:22:27             🧑  作者: Mango

给定一个正整数n,对x进行计数,使0 n,其中^是按位XOR运算。
例子:

Input  : n = 12
Output : 3
Numbers are 1, 2 and 3
1^12 > 12,  2^12 > 12 and 3^12 > 12

Input  : n = 11
Output : 4
Numbers are 4, 5, 6 and 7

如果x在n的位置为0的位置处置位,则x的值可能会产生更大的XOR值。因此,我们遍历n位,然后一一考虑所有0位。对于位置k处的每个置位(考虑到k = 0,表示最右边的位,k = 1表示第二个最右边的位,..),我们将结果加2 2 k 。对于第k位的位,设置位1为2 k个数字。
以下是上述想法的实现。

C++
// C++ program to count numbers whose XOR with n
// produces a value more than n.
#include
using namespace std;
 
int countNumbers(int n)
{
    /* If there is a number like m = 11110000,
    then it is bigger then 1110xxxx. x can either
    0 or 1. So we have pow(2, k) greater numbers
    where k is  position of rightmost 1 in m. Now
    by using XOR bit at each  position can be changed.
    To change bit at any position, it needs to XOR
    it with 1. */
    int k = 0; // Position of current bit in n
 
    /* Traverse bits from LSB (least significant bit)
       to MSB */
    int count = 0;  // Initialize result
    while (n > 0)
    {
        // If current bit is 0, then there are
        // 2^k numbers with current bit 1 and
        // whose XOR with n produces greater value
        if ((n&1) == 0)
            count += pow(2, k);
 
        // Increase position for next bit
        k += 1;
 
        // Reduce n to find next bit
        n >>= 1;
    }
 
    return count;
}
 
// Driver code
int main()
{
    int n = 11;
    cout << countNumbers(n) << endl;
    return 0;
}


Java
// Java program to count numbers
// whose XOR with n produces a
// value more than n.
import java.lang.*;
class GFG {
 
    static int countNumbers(int n)
    {
 
        // If there is a number like
        // m = 11110000, then it is
        // bigger than 1110xxxx. x
        // can either be 0 or 1. So
        // where k is the position of
        // rightmost 1 in m. Now by
        // using the XOR bit at each
        // position can be changed.
        // To change the bit at any
        // position, it needs to
        // XOR it with 1.
        int k = 0;
        // Position of current bit in n
 
        // Traverse bits from LSB (least
        // significant bit) to MSB
 
        int count = 0;
        // Initialize result
        while (n > 0) {
            // If the current bit is 0, then
            // there are 2^k numbers with
            // current bit 1 and whose XOR
            // with n produces greater value
            if ((n & 1) == 0)
                count += (int)(Math.pow(2, k));
 
            // Increase position for next bit
            k += 1;
 
            // Reduce n to find next bit
            n >>= 1;
        }
 
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 11;
        System.out.println(countNumbers(n));
    }
}
 
// This code is contributed by Smitha.


Python3
# Python program to count numbers whose
# XOR with n produces a value more than n.
 
def countNumbers(n):
 
    ''' If there is a number like m = 11110000,
    then it is bigger then 1110xxxx. x can either
    0 or 1. So we have pow(2, k) greater numbers
    where k is position of rightmost 1 in m. Now
    by using XOR bit at each position can be changed.
    To change bit at any position, it needs to XOR
    it with 1. '''
     
    # Position of current bit in n
    k = 0
 
    # Traverse bits from
    # LSB to MSB
    count = 0 # Initialize result
    while (n > 0):
     
        # If current bit is 0, then there are
        # 2^k numbers with current bit 1 and
        # whose XOR with n produces greater value
        if ((n & 1) == 0):
            count += pow(2, k)
 
        # Increase position for next bit
        k += 1
 
        # Reduce n to find next bit
        n >>= 1
 
    return count
 
# Driver code
n = 11
print(countNumbers(n))
 
# This code is contributed by Anant Agarwal.


C#
// C# program to count numbers
// whose XOR with n produces a
// value more than n.
using System;
 
class GFG {
 
    static int countNumbers(int n)
    {
 
        // If there is a number like
        // m = 11110000, then it is
        // bigger than 1110xxxx. x
        // can either be 0 or 1. So
        // where k is the position of
        // rightmost 1 in m. Now by
        // using the XOR bit at each
        // position can be changed.
        // To change the bit at any
        // position, it needs to
        // XOR it with 1.
        int k = 0;
        // Position of current bit in n
 
        // Traverse bits from LSB (least
        // significant bit) to MSB
 
        int count = 0;
        // Initialize result
        while (n > 0) {
            // If the current bit is 0, then
            // there are 2^k numbers with
            // current bit 1 and whose XOR
            // with n produces greater value
            if ((n & 1) == 0)
                count += (int)(Math.Pow(2, k));
 
            // Increase position for next bit
            k += 1;
 
            // Reduce n to find next bit
            n >>= 1;
        }
 
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 11;
        Console.WriteLine(countNumbers(n));
    }
}
 
// This code is contributed by Anant Agarwal.


PHP
 0)
    {
         
        // If current bit is 0,
        // then there are 2^k
        // numbers with current
        // bit 1 and whose XOR
        // with n produces greater
        // value
        if (($n & 1) == 0)
            $count += pow(2, $k);
 
        // Increase position
        // for next bit
        $k += 1;
 
        // Reduce n to
        // find next bit
        $n >>= 1;
    }
 
    return $count;
}
 
    // Driver code
    $n = 11;
    echo countNumbers($n);
 
// This code is contributed by anuj_67.
?>


Javascript


输出:

4