Count of pairs having bit size at most X and Bitwise OR equal to X | Java 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
class GFG { static int count_pairs ( int x ) { int ans = 1 ; while ( x > 0 ) { if ( x % 2 == 1 ) ans = ans * 3 ; x = x / 2 ; } return ans ; } public static void main ( String [ ] args ) { int X = 6 ; System . out . print ( count_pairs ( X ) + "NEW_LINE"); } }