Count of pairs having bit size at most X and Bitwise OR equal to X | C ++ implementation to Count number of possible pairs of ( a , b ) such that their Bitwise OR gives the value X ; Function to count the pairs ; Initializing answer with 1 ; Iterating through bits of x ; check if bit is 1 ; multiplying ans by 3 if bit is 1 ; Driver code
#include <iostream> NEW_LINE using namespace std ; int count_pairs ( int x ) { int ans = 1 ; while ( x > 0 ) { if ( x % 2 == 1 ) ans = ans * 3 ; x = x / 2 ; } return ans ; } int main ( ) { int X = 6 ; cout << count_pairs ( X ) << endl ; return 0 ; }