Equal Sum and XOR | c ++ program to print count of values such that n + i = n ^ i ; function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; Return 2 ^ unset_bits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countValues ( int n ) { int unset_bits = 0 ; while ( n ) { if ( ( n & 1 ) == 0 ) unset_bits ++ ; n = n >> 1 ; } return 1 << unset_bits ; } int main ( ) { int n = 12 ; cout << countValues ( n ) ; return 0 ; }