# Count numbers whose XOR with N is equal to OR with N

Given a number N, the task is to find the count of X such that **N XOR X** == **N OR X**, where 0<=X<=N

**Examples:**

Input: N = 5

Output: 2

For N = 5,

5 XOR 2 == 5 OR 2

5 XOR 0 == 5 OR 0

Thus, count is 2.

Input: N = 7

Output: 1

For N = 7,

7 XOR 0 == 7 OR 0

Thus, count is 1.

**Approach:** The idea is to convert given number to binary and then count the unset bits in it. **2^count** gives us the number of X such that **N XOR X** == **N OR X**.

Below is the implementation of the above approach:

## C++

`// C++ program to find ` `// the XOR equals OR count ` `#include<iostream> ` `#include<math.h> ` `using` `namespace` `std; ` ` ` `class` `gfg { ` ` ` ` ` `// Function to calculate count ` ` ` `// of numbers with XOR equals OR ` ` ` `public` `: ` ` ` `int` `xorEqualsOrCount(` `int` `N) ` ` ` `{ ` ` ` ` ` `// variable to store count of unset bits ` ` ` `int` `count = 0; ` ` ` `int` `bit; ` ` ` `while` `(N > 0) { ` ` ` ` ` `bit = N % 2; ` ` ` `if` `(bit == 0) ` ` ` `count++; ` ` ` `N = N / 2; ` ` ` `} ` ` ` `return` `(` `int` `)` `pow` `(2, count); ` ` ` `} }; ` ` ` ` ` `// Driver code ` ` ` `int` `main() ` ` ` `{ ` ` ` `gfg g ; ` ` ` `int` `N = 7; ` ` ` `cout<<g.xorEqualsOrCount(N); ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// This code is contributed by Soumik ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the XOR equals OR count ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate count of numbers with XOR equals OR ` ` ` `static` `int` `xorEqualsOrCount(` `int` `N) ` ` ` `{ ` ` ` `// variable to store count of unset bits ` ` ` `int` `count = ` `0` `; ` ` ` `int` `bit; ` ` ` `while` `(N > ` `0` `) { ` ` ` `bit = N % ` `2` `; ` ` ` `if` `(bit == ` `0` `) ` ` ` `count++; ` ` ` `N = N / ` `2` `; ` ` ` `} ` ` ` `return` `(` `int` `)Math.pow(` `2` `, count); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `N = ` `7` `; ` ` ` `System.out.println(xorEqualsOrCount(N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find ` `# the XOR equals OR count ` ` ` `# Function to calculate count ` `# of numbers with XOR equals OR ` `def` `xorEqualsOrCount(N) : ` ` ` ` ` `# variable to store ` ` ` `# count of unset bits ` ` ` `count ` `=` `0` ` ` ` ` `while` `(N > ` `0` `) : ` ` ` ` ` `bit ` `=` `N ` `%` `2` ` ` ` ` `if` `bit ` `=` `=` `0` `: ` ` ` `count ` `+` `=` `1` ` ` ` ` `N ` `/` `/` `=` `2` ` ` ` ` `return` `int` `(` `pow` `(` `2` `, count)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `7` ` ` `print` `(xorEqualsOrCount(N)) ` ` ` `# This code is contributed by ` `# ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find ` `// the XOR equals OR count ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate count ` ` ` `// of numbers with XOR equals OR ` ` ` `static` `int` `xorEqualsOrCount(` `int` `N) ` ` ` `{ ` ` ` ` ` `// variable to store count of unset bits ` ` ` `int` `count = 0; ` ` ` `int` `bit; ` ` ` `while` `(N > 0) { ` ` ` ` ` `bit = N % 2; ` ` ` `if` `(bit == 0) ` ` ` `count++; ` ` ` `N = N / 2; ` ` ` `} ` ` ` `return` `(` `int` `)Math.Pow(2, count); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 7; ` ` ` `Console.WriteLine(xorEqualsOrCount(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the XOR ` `// equals OR count ` ` ` `// Function to calculate count ` `// of numbers with XOR equals OR ` `function` `xorEqualsOrCount(` `$N` `) ` `{ ` ` ` ` ` `// variable to store count ` ` ` `// of unset bits ` ` ` `$count` `= 0; ` ` ` `while` `(` `$N` `> 0) ` ` ` `{ ` ` ` `$bit` `= ` `$N` `% 2; ` ` ` `if` `(` `$bit` `== 0) ` ` ` `$count` `++; ` ` ` `$N` `= ` `intval` `(` `$N` `/ 2); ` ` ` `} ` ` ` `return` `pow(2, ` `$count` `); ` `} ` ` ` `// Driver code ` `$N` `= 7; ` `echo` `xorEqualsOrCount(` `$N` `); ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Count numbers whose sum with x is equal to XOR with x
- Count numbers whose difference with N is equal to XOR with N
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count the numbers < N which have equal number of divisors as K
- Generate an array of given size with equal count and sum of odd and even numbers
- Count pairs of natural numbers with GCD equal to given number
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Maximum count of equal numbers in an array after performing given operations
- Count of Numbers in Range where first digit is equal to last digit of the number
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Equal Sum and XOR of three Numbers
- Numbers whose bitwise OR and sum with N are equal
- Count of Multiples of A ,B or C less than or equal to N
- Count subarrays with sum equal to its XOR value
- Make all numbers of an array equal
- Find all factorial numbers less than or equal to n
- Sum of Semi-Prime Numbers less than or equal to N
- Minimum numbers (smaller than or equal to N) with sum S

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.