Count smaller numbers whose XOR with n produces greater value | C ++ program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; int count = 0 ; Initialize result ; If current bit is 0 , then there are 2 ^ k numbers with current bit 1 and whose XOR with n produces greater value ; Increase position for next bit ; Reduce n to find next bit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int n ) { int k = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count += pow ( 2 , k ) ; k += 1 ; n >>= 1 ; } return count ; } int main ( ) { int n = 11 ; cout << countNumbers ( n ) << endl ; return 0 ; }