Given a number N. The task is to count the all possible values of x such that nx is equal to (N-x), where denotes bitwise XOR operation.

**Examples:**

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.

**Approach:** The XOR value of two bits will be 1 if both bits have opposite sign, and 0 when both bits are same. So on the basis of the property of XOR, we can say that n x is always greater than or equal to n-x. The only condition when its value is equal with n-x is bits of x form a subset of bits of n. Because if in the i’th position both **x** and **n** has set bits then after xor the value will decrease, and the decreased value will be , where **i** is 0-based position.

So the answer is the total count of subsets of bits of number n is , where k is the count of set bits in n.

Below is the implementation of above approach:

## C++

`#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// count number of set bits ` `function` `__builtin_popcount(` `$n` `) ` `{ ` ` ` `// count variable ` ` ` `$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 ` `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) ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

16

**Time Complexity:** O(k), where k is number of set bits in N.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count numbers whose sum with x is equal to XOR with x
- Count numbers whose XOR with N is equal to OR with N
- Count smaller numbers whose XOR with n produces greater value
- Find an N x N grid whose xor of every row and column is equal
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Find two numbers whose difference of fourth power is equal to N
- Count of pairs of integers whose difference of squares is equal to N
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count smaller values whose XOR with x is greater than x
- Count all pairs of adjacent nodes whose XOR is an odd number
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- XOR of two numbers after making length of their binary representations equal
- Equal Sum and XOR of three Numbers
- Count minimum bits to flip such that XOR of A and B equal to C
- Count of elements which are equal to the XOR of the next two elements
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Count subarrays with sum equal to its XOR value

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.