# Count numbers whose sum with x is equal to XOR with x

Given a integer ‘x’, find the number of values of ‘a’ satisfying the following conditions:

1. 0 <= a <= x
2. a XOR x = a + x

Examples :

```Input : 5
Output : 2
Explanation: For x = 5, following 2 values
of 'a' satisfy the conditions:
5 XOR 0 = 5+0
5 XOR 2 = 5+2
Input : 10
Output : 4
Explanation: For x = 10, following 4 values
of 'a' satisfy the conditions:
10 XOR 0 = 10+0
10 XOR 1 = 10+1
10 XOR 4 = 10+4
10 XOR 5 = 10+5
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach:
A Simple approach is to check for all values of ‘a’ between 0 and ‘x’ (both inclusive) and calculate its XOR with x and check if the condition 2 satisfies.

 `// C++ program to find count of values whose XOR ` `// with x is equal to the sum of value and x ` `// and values are smaller than equal to x ` `#include ` `using` `namespace` `std; ` ` `  `int` `FindValues(``int` `x) ` `{ ` `    ``// Initialize result ` `    ``int` `count = 0; ` ` `  `    ``// Traversing through all values between ` `    ``// 0 and x both inclusive and counting ` `    ``// numbers that satisfy given property ` `    ``for` `(``int` `i=0; i<=x; i++) ` `        ``if` `((x+i) == (x^i)) ` `            ``count++; ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `x = 10; ` `    ``cout << FindValues(x); ` `    ``return` `0; ` `} `

 `// Java program to find count of values whose XOR ` `// with x is equal to the sum of value and x ` `// and values are smaller than equal to x ` ` `  `class` `Fib ` `{ ` `    ``static` `int` `FindValues(``int` `x) ` `    ``{ ` `        ``// Initialize result ` `        ``int` `count = ``0``; ` `      `  `        ``// Traversing through all values between ` `        ``// 0 and x both inclusive and counting ` `        ``// numbers that satisfy given property ` `        ``for` `(``int` `i=``0``; i<=x; i++) ` `            ``if` `((x+i) == (x^i)) ` `                ``count++; ` `      `  `        ``return` `count; ` `    ``} ` `     `  `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `x=``10``; ` `        ``System.out.println(FindValues(x)); ` `    ``} ` `} `

 `# Python3 program to find count of  ` `# values whose XOR with x is equal  ` `# to the sum of value and x and  ` `# values are smaller than equal to x ` ` `  `def` `FindValues( x): ` ` `  `    ``# Initialize result ` `    ``count ``=` `0``; ` ` `  `    ``# Traversing through all values  ` `    ``# between 0 and x both inclusive  ` `    ``# and counting numbers that ` `    ``# satisfy given property ` `    ``for` `i ``in` `range``(``0``, x): ` `        ``if` `((x ``+` `i) ``=``=` `(x ^ i)): ` `            ``count ``=` `count ``+` `1``; ` ` `  `    ``return` `count; ` ` `  `# Driver code ` `x ``=` `10``; ` `print` `(FindValues(x)); ` ` `  `# This code is contributed  ` `# by Shivi_Aggarwal `

 `// C# program to find count of values whose XOR ` `// with x is equal to the sum of value and x ` `// and values are smaller than equal to x ` `using` `System; ` ` `  `class` `Fib ` `{ ` `    ``static` `int` `FindValues(``int` `x) ` `    ``{ ` `        ``// Initialize result ` `        ``int` `count = 0; ` `     `  `        ``// Traversing through all values between ` `        ``// 0 and x both inclusive and counting ` `        ``// numbers that satisfy given property ` `        ``for` `(``int` `i = 0; i <= x; i++) ` `            ``if` `((x+i) == (x^i)) ` `                ``count++; ` `     `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `x = 10; ` `        ``Console.Write(FindValues(x)); ` `    ``} ` `} ` ` `  `// This code is contributed by Nitin Mittal. `

 ` `

Output :

```4
```

The time complexity of the above approach is O(x).

Efficient Approach:
XOR simulates binary addition without the carry over to the next digit. For the zero digits of ‘a’ we can either add a 1 or 0 without getting a carry which implies xor = + whereas if a digit in ‘a’ is 1 then the matching digit in x is forced to be 0 in order to avoid carry. For each 0 in ‘a’ in the matching digit in x can either being a 1 or 0 with a total combination count of 2^(num of zero). Hence, we just need to count the number of 0’s in binary representation of the number and answer will by 2^(number of zeroes).

 `// C++ program to count numbers whose bitwise ` `// XOR and sum with x are equal ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find total 0 bit in a number ` `unsigned ``int` `CountZeroBit(``int` `x) ` `{ ` `    ``unsigned ``int` `count = 0; ` `    ``while` `(x) ` `    ``{ ` `       ``if` `(!(x & 1)) ` `           ``count++; ` `       ``x >>= 1; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Function to find Count of non-negative numbers ` `// less than or equal to x, whose bitwise XOR and ` `// SUM with x are equal. ` `int` `CountXORandSumEqual(``int` `x) ` `{ ` `    ``// count number of zero bit in x ` `    ``int` `count = CountZeroBit(x); ` ` `  `    ``// power of 2 to count ` `    ``return` `(1 << count); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `   ``int` `x = 10; ` `   ``cout << CountXORandSumEqual(x); ` `   ``return` `0; ` `} `

 `// Java program to count  ` `// numbers whose bitwise ` `// XOR and sum with x  ` `// are equal ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `// Function to find total  ` `// 0 bit in a number ` `static` `int` `CountZeroBit(``int` `x) ` `{ ` `    ``int` `count = ``0``; ` `    ``while` `(x > ``0``) ` `    ``{ ` `        ``if` `((x & ``1``) == ``0``) ` `            ``count++; ` `        ``x >>= ``1``; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Function to find Count  ` `// of non-negative numbers ` `// less than or equal to x, ` `// whose bitwise XOR and ` `// SUM with x are equal. ` `static` `int` `CountXORandSumEqual(``int` `x) ` `{ ` `    ``// count number of ` `    ``// zero bit in x ` `    ``int` `count = CountZeroBit(x); ` ` `  `    ``// power of 2 to count ` `    ``return` `(``1` `<< count); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `     `  `    ``int` `x = ``10``; ` `    ``System.out.println(CountXORandSumEqual(x)); ` `} ` `} ` ` `  `// The code is contributed by ajit `

 `# Python3 program to count numbers whose bitwise ` `# XOR and sum with x are equal ` ` `  `# Function to find total 0 bit in a number ` `def` `CountZeroBit(x): ` ` `  `    ``count ``=` `0``; ` `    ``while` `(x): ` `     `  `        ``if` `((x & ``1``) ``=``=` `0``): ` `            ``count ``+``=` `1``; ` `        ``x >>``=` `1``; ` `     `  `    ``return` `count; ` ` `  `# Function to find Count of non-negative numbers ` `# less than or equal to x, whose bitwise XOR and ` `# SUM with x are equal. ` `def` `CountXORandSumEqual(x): ` ` `  `    ``# count number of zero bit in x ` `    ``count ``=` `CountZeroBit(x); ` ` `  `    ``# power of 2 to count ` `    ``return` `(``1` `<< count); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``x ``=` `10``; ` `    ``print``(CountXORandSumEqual(x)); ` ` `  `# This code is contributed by 29AjayKumar `

 `// C# program to count  ` `// numbers whose bitwise ` `// XOR and sum with x  ` `// are equal ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to find total  ` `// 0 bit in a number ` `static` `int` `CountZeroBit(``int` `x) ` `{ ` `    ``int` `count = 0; ` `    ``while` `(x > 0) ` `    ``{ ` `        ``if` `((x & 1) == 0) ` `            ``count++; ` `        ``x >>= 1; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Function to find Count  ` `// of non-negative numbers ` `// less than or equal to x, ` `// whose bitwise XOR and ` `// SUM with x are equal. ` `static` `int` `CountXORandSumEqual(``int` `x) ` `{ ` `    ``// count number of ` `    ``// zero bit in x ` `    ``int` `count = CountZeroBit(x); ` ` `  `    ``// power of 2 to count ` `    ``return` `(1 << count); ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `x = 10; ` `    ``Console.WriteLine(CountXORandSumEqual(x)); ` `} ` `} ` ` `  `// The code is contributed by ajit `

 `>= 1; ` `    ``} ` `    ``return` `\$count``; ` `} ` ` `  `// Function to find Count of ` `// non-negative numbers less ` `// than or equal to x, whose ` `// bitwise XOR and SUM with ` `// x are equal. ` `function` `CountXORandSumEqual(``\$x``) ` `{ ` `    ``// count number of zero bit in x ` `    ``\$count` `= CountZeroBit(``\$x``); ` ` `  `    ``// power of 2 to count ` `    ``return` `(1 << ``\$count``); ` `} ` ` `  `    ``// Driver code ` `    ``\$x` `= 10; ` `    ``echo` `CountXORandSumEqual(``\$x``); ` ` `  `// This code is contributed by m_kit ` `?> `

Output:
```4
```

Time complexity of this solution is O(Log x)

This article is contributed by DANISH KALEEM. 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.