The Wayback Machine - https://web.archive.org/web/20220224185743/https://www.geeksforgeeks.org/count-of-all-possible-values-of-x-whose-bitwise-xor-with-n-is-greater-than-n/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count of all possible values of X whose Bitwise XOR with N is greater than N

  • Last Updated : 24 Feb, 2022

Given an integer N count the number of values of X such that  X⊕N > N, where  ⊕ denotes bitwise  XOR operation

Examples:

Input: N = 10
Output: 5 
Explanation: The five possible value satisfying the above condition are:
1⊕10 = 11, 4⊕10 = 14, 5⊕10 = 15, 6⊕10 = 12, 7⊕10 = 13

Input: N = 8
Output: 7
Explanation: The seven possible value satisfying the above condition are:
1⊕8 = 9, 2⊕8 = 10, 3⊕8 = 11, 4⊕8 = 12, 5⊕8 = 13, 6⊕8 = 14, 7⊕8 = 15

 

Naive Approach: The simple approach to this problem is to iterate from 1 to N and increment the count if  X XOR N >N  for 0 < X < N. Finally, return the count of the number of values.

Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach: The most efficient approach is to find the number nearest to the next power of 2 which has all its bits set and finally subtract it from the original number. Given below is the mathematical observation of the solution:

If number N can be written by using k bits then number  X must use k-1 bits at most because:-

  • If a number X has a same bits as number N Xoring both the number will result in a number less than N which wont satisfy our condition X xor N > N.
  • If a number X is greater than number N then Xoring both the number  will always result X is greater than N which wont satisfy our inequality.
    So the problem reduces to find the count of the number that lies in the given range [x , 2k – 1]. We are taking 2k – 1 because this is the maximum number that can be form when all its n bits are set.
  • The require number equals  2k – 1 -N .

Follow the steps below to solve the problem:

  • Find the nearest next power of 2.
  • Subtract 1 from the nearest power of 2  so that the given number has all its bit set
  • Finally, subtract from the original number and return it

Below is the implementation of the above approach: 

C++




// C++ program for find the count of
// number of value of X such that  N XOR X >N
  
#include <bits/stdc++.h>
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;
}
Output
5

Time Complexity: O(log(N))
Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!