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

- a XOR x > x
- 0 < a < x

**Examples :**

Input : x = 10 Output : 5 Explanation: For x = 10, following 5 values of 'a' satisfy the conditions: 1 XOR 10 = 11 4 XOR 10 = 14 5 XOR 10 = 15 6 XOR 10 = 12 7 XOR 10 = 13 Input : x = 2 Output : 1 Explanation: For x=2, we have just one value 1 XOR 2 = 3.

**Naive Approach**

A Simple approach is to check for all values of ‘a’ between 0 and ‘x’ and calculate its XOR with x

and check if the condition 1 satisfies.

## C++

`// C++ program to find count of values ` `// whose XOR with x is greater than x ` `// and values are smaller than x ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countValues(` `int` `x) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i=1; i < x; i++) ` ` ` `if` `((i ^ x) > x) ` ` ` `count++; ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 10; ` ` ` `cout << countValues(x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find count of values ` `// whose XOR with x is greater than x ` `// and values are smaller than x ` ` ` `public` `class` `XOR ` `{ ` ` ` `static` `int` `countValues(` `int` `x) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i=` `1` `; i < x; i++) ` ` ` `if` `((i ^ x) > x) ` ` ` `count++; ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `x = ` `10` `; ` ` ` `System.out.println(countValues(x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Saket Kumar ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 program to find ` `# count of values whose ` `# XOR with x is greater ` `# than x and values are ` `# smaller than x ` ` ` `def` `countValues(x): ` ` ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `,x): ` ` ` `if` `((i ^ x) > x): ` ` ` `count ` `+` `=` `1` ` ` `return` `count ` ` ` `# Driver code ` `x ` `=` `10` `print` `(countValues(x)) ` ` ` `# This code is contributed ` `# by Smitha ` |

*chevron_right*

*filter_none*

## C#

` ` `// C# program to find count of values ` `// whose XOR with x is greater than x ` `// and values are smaller than x ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `countValues(` `int` `x) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 1; i < x; i++) ` ` ` `if` `((i ^ x) > x) ` ` ` `count++; ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `x = 10; ` ` ` `Console.Write(countValues(x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find count of values ` `// whose XOR with x is greater than x ` `// and values are smaller than x ` ` ` `function` `countValues(` `$x` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$x` `; ` `$i` `++) ` ` ` `if` `((` `$i` `^ ` `$x` `) > ` `$x` `) ` ` ` `$count` `++; ` ` ` `return` `$count` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `$x` `= 10; ` ` ` `echo` `countValues(` `$x` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5

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

**Efficient Approach**

The efficient solution lies in the binary representation of the number. We consider all 0’s in binary representation. For every 0 at the i-th position, we can have 2^{i} numbers smaller than or equal to x with greater XOR.

## C++

`// C++ program to find count of values ` `// whose XOR with x is greater than x ` `// and values are smaller than x ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countValues(` `int` `x) ` `{ ` ` ` `// Initialize result ` ` ` `int` `count = 0, n = 1; ` ` ` ` ` `// Traversing through all bits of x ` ` ` `while` `(x != 0) ` ` ` `{ ` ` ` `// If current last bit of x is set ` ` ` `// then increment count by n. Here ` ` ` `// n is a power of 2 corresponding ` ` ` `// to position of bit ` ` ` `if` `(x%2 == 0) ` ` ` `count += n; ` ` ` ` ` `// Simultaneously calculate the 2^n ` ` ` `n *= 2; ` ` ` ` ` `// Replace x with x/2; ` ` ` `x /= 2; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 10; ` ` ` `cout << countValues(x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find count of values ` `// whose XOR with x is greater than x ` `// and values are smaller than x ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `countValues(` `int` `x) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `count = ` `0` `, n = ` `1` `; ` ` ` ` ` `// Traversing through all bits of x ` ` ` `while` `(x != ` `0` `) ` ` ` `{ ` ` ` `// If current last bit of x is set ` ` ` `// then increment count by n. Here ` ` ` `// n is a power of 2 corresponding ` ` ` `// to position of bit ` ` ` `if` `(x % ` `2` `== ` `0` `) ` ` ` `count += n; ` ` ` ` ` `// Simultaneously calculate the 2^n ` ` ` `n *= ` `2` `; ` ` ` ` ` `// Replace x with x/2; ` ` ` `x /= ` `2` `; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `x = ` `10` `; ` ` ` `System.out.println(countValues(x)); ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by Saket Kumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find count ` `# of values whose XOR with ` `# x is greater than x and ` `# values are smaller than x ` ` ` `def` `countValues(x): ` ` ` ` ` `# Initialize result ` ` ` `count ` `=` `0` `; ` ` ` `n ` `=` `1` `; ` ` ` ` ` `# Traversing through ` ` ` `# all bits of x ` ` ` `while` `(x > ` `0` `): ` ` ` ` ` `# If current last bit ` ` ` `# of x is set then ` ` ` `# increment count by ` ` ` `# n. Here n is a power ` ` ` `# of 2 corresponding ` ` ` `# to position of bit ` ` ` `if` `(x ` `%` `2` `=` `=` `0` `): ` ` ` `count ` `+` `=` `n; ` ` ` ` ` `# Simultaneously ` ` ` `# calculate the 2^n ` ` ` `n ` `*` `=` `2` `; ` ` ` ` ` `# Replace x with x/2; ` ` ` `x ` `/` `=` `2` `; ` ` ` `x ` `=` `int` `(x); ` ` ` ` ` `return` `count; ` ` ` `# Driver code ` `x ` `=` `10` `; ` `print` `(countValues(x)); ` ` ` `# This code is contributed ` `# by mits ` |

*chevron_right*

*filter_none*

## C#

` ` `// C# program to find count of values ` `// whose XOR with x is greater than x ` `// and values are smaller than x ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `countValues(` `int` `x) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `count = 0, n = 1; ` ` ` ` ` `// Traversing through all bits of x ` ` ` `while` `(x != 0) ` ` ` `{ ` ` ` `// If current last bit of x is set ` ` ` `// then increment count by n. Here ` ` ` `// n is a power of 2 corresponding ` ` ` `// to position of bit ` ` ` `if` `(x % 2 == 0) ` ` ` `count += n; ` ` ` ` ` `// Simultaneously calculate the 2^n ` ` ` `n *= 2; ` ` ` ` ` `// Replace x with x/2; ` ` ` `x /= 2; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `x = 10; ` ` ` `Console.Write(countValues(x)); ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find count ` `// of values whose XOR with ` `// x is greater than x and ` `// values are smaller than x ` ` ` `function` `countValues(` `$x` `) ` `{ ` ` ` ` ` `// Initialize result ` ` ` `$count` `= 0; ` ` ` `$n` `= 1; ` ` ` ` ` `// Traversing through ` ` ` `// all bits of x ` ` ` `while` `(` `$x` `!= 0) ` ` ` `{ ` ` ` `// If current last bit ` ` ` `// of x is set then ` ` ` `// increment count by ` ` ` `// n. Here n is a power ` ` ` `// of 2 corresponding ` ` ` `// to position of bit ` ` ` `if` `(` `$x` `% 2 == 0) ` ` ` `$count` `+= ` `$n` `; ` ` ` ` ` `// Simultaneously ` ` ` `// calculate the 2^n ` ` ` `$n` `*= 2; ` ` ` ` ` `// Replace x with x/2; ` ` ` `$x` `/= 2; ` ` ` `$x` `= (int)` `$x` `; ` ` ` `} ` ` ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$x` `= 10; ` `echo` `countValues(` `$x` `); ` ` ` `// This code is contributed ` `// by Smitha ` `?> ` |

*chevron_right*

*filter_none*

Output :

Output :

5

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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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 smaller numbers whose XOR with n produces greater value
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Closest (or Next) smaller and greater numbers with same number of set bits
- Count of values of x <= n for which (n XOR x) = (n - x)
- Count subsequences with same values of Bitwise AND, OR and XOR
- Queries for count of array elements with values in given range with updates
- Range Queries to count the number of even parity values with updates
- 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
- Round to next smaller multiple of 8
- Previous smaller integer having one less number of set bits
- Find the largest number smaller than integer N with maximum number of set bits
- Round to next greater multiple of 8
- Next greater number than N with exactly one bit different in binary representation of N
- Next greater integer having one more number of set bits
- Smallest power of 2 greater than or equal to n
- Minimum count of Full Binary Trees such that the count of leaves is N
- Number of values of b such that a = b + (a^b)
- Smallest number greater or equals to N such that it has no odd positioned bit set
- Check if a number from every row can be selected such that xor of the numbers is greater than zero
- Count of binary strings of length N having equal count of 0's and 1's