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