Given a number, the task is to find XOR of count of 0s and count of 1s in binary representation of a given number.

**Examples:**

Input : 5 Output : 3 Binary representation : 101 Count of 0s = 1, Count of 1s = 2 1 XOR 2 = 3. Input : 7 Output : 3 Binary representation : 111 Count of 0s = 0 Count of 1s = 3 0 XOR 3 = 3.

The idea is simple, we traverse through all bits of a number, count 0s and 1s and finally return XOR of two counts.

## C++

`// C++ program to find XOR of counts 0s and 1s in ` `// binary representation of n. ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `// Returns XOR of counts 0s and 1s in ` `// binary representation of n. ` `int` `countXOR(` `int` `n) ` `{ ` ` ` `int` `count0 = 0, count1 = 0; ` ` ` `while` `(n) ` ` ` `{ ` ` ` `//calculating count of zeros and ones ` ` ` `(n % 2 == 0) ? count0++ :count1++; ` ` ` `n /= 2; ` ` ` `} ` ` ` `return` `(count0 ^ count1); ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `n = 31; ` ` ` `cout << countXOR (n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find XOR of counts 0s ` `// and 1s in binary representation of n. ` ` ` `class` `GFG { ` ` ` ` ` `// Returns XOR of counts 0s and 1s ` ` ` `// in binary representation of n. ` ` ` `static` `int` `countXOR(` `int` `n) ` ` ` `{ ` ` ` `int` `count0 = ` `0` `, count1 = ` `0` `; ` ` ` `while` `(n != ` `0` `) ` ` ` `{ ` ` ` `//calculating count of zeros and ones ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `count0++ ; ` ` ` `else` ` ` `count1++; ` ` ` `n /= ` `2` `; ` ` ` `} ` ` ` `return` `(count0 ^ count1); ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `31` `; ` ` ` `System.out.println(countXOR (n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by prerna saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find XOR of counts 0s ` `# and 1s in binary representation of n. ` ` ` `# Returns XOR of counts 0s and 1s ` `# in binary representation of n. ` `def` `countXOR(n): ` ` ` ` ` `count0, count1 ` `=` `0` `, ` `0` ` ` `while` `(n !` `=` `0` `): ` ` ` ` ` `# calculating count of zeros and ones ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `count0 ` `+` `=` `1` ` ` `else` `: ` ` ` `count1 ` `+` `=` `1` ` ` `n ` `/` `/` `=` `2` ` ` ` ` `return` `(count0 ^ count1) ` ` ` `# Driver Code ` `n ` `=` `31` `print` `(countXOR(n)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find XOR of counts 0s ` `// and 1s in binary representation of n. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns XOR of counts 0s and 1s ` ` ` `// in binary representation of n. ` ` ` `static` `int` `countXOR(` `int` `n) ` ` ` `{ ` ` ` `int` `count0 = 0, count1 = 0; ` ` ` `while` `(n != 0) ` ` ` `{ ` ` ` ` ` `// calculating count of zeros ` ` ` `// and ones ` ` ` `if` `(n % 2 == 0) ` ` ` `count0++ ; ` ` ` `else` ` ` `count1++; ` ` ` ` ` `n /= 2; ` ` ` `} ` ` ` ` ` `return` `(count0 ^ count1); ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `n = 31; ` ` ` ` ` `Console.WriteLine(countXOR (n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?PHP ` `// PHP program to find XOR of ` `// counts 0s and 1s in binary ` `// representation of n. ` ` ` `// Returns XOR of counts 0s and 1s ` `// in binary representation of n. ` `function` `countXOR(` `$n` `) ` `{ ` ` ` `$count0` `= 0; ` ` ` `$count1` `= 0; ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `// calculating count of ` ` ` `// zeros and ones ` ` ` `(` `$n` `% 2 == 0) ? ` `$count0` `++ :` `$count1` `++; ` ` ` `$n` `= ` `intval` `(` `$n` `/ 2); ` ` ` `} ` ` ` `return` `(` `$count0` `^ ` `$count1` `); ` `} ` ` ` `// Driver Code ` `$n` `= 31; ` `echo` `countXOR (` `$n` `); ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5

One observation is, for a number of the form **2^x – 1**, the output is always x. We can directly produce answer for this case by first checking n+1 is a power of two or not.

This article is contributed by **Shivam Pradhan (anuj_charm)**. 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 integers in an Array which are multiples their bits counts
- Check if binary representation of a given number and its complement are anagram
- Largest number with binary representation is m 1's and m-1 0's
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Find the number obtained after concatenation of binary representation of M and N
- Convert numbers into binary representation and add them without carry
- C program to count zeros and ones in binary representation of a number
- Find XOR of two number without using XOR operator
- Given a set, find XOR of the XOR's of all subsets.
- Choose X such that (A xor X) + (B xor X) is minimized
- Binary representation of a given number
- Check if binary representation of a number is palindrome
- Length of the Longest Consecutive 1s in Binary Representation
- Maximum 0's between two immediate 1's in binary representation
- Find longest sequence of 1's in binary representation with one flip
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Binary representation of previous number
- Decimal representation of given binary string is divisible by 10 or not
- Check if actual binary representation of a number is palindrome
- Decimal representation of given binary string is divisible by 20 or not