Count of pairs having bit size at most X and Bitwise OR equal to X

Given a number X, calculate number of possible pairs (a, b) such that bitwise or of a and b is equal to X and number of bits in both a and b is less than equal to number of bits in X.

Examples: 

Input: X = 6 
Output:
Explanation: 
The possible pairs of (a, b) are (4, 6), (6, 4), (6, 6), (6, 2), (4, 2), (6, 0), (2, 6), (2, 4), (0, 6).

Input: X = 21 
Output: 27 
Explanation: 
In total there are 27 pairs possible.

Approach: To solve the problem mentioned above follow the steps given below:



Below is the implementation of above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
class GFG{
  
// Function to count the pairs
static int count_pairs(int x)
{
      
    // Initializing answer with 1
    int ans = 1;
  
    // Iterating through bits of x
    while (x > 0)
    {
          
        // Check if bit is 1
        if (x % 2 == 1)
  
            // Multiplying ans by 3
            // if bit is 1
            ans = ans * 3;
  
        x = x / 2;
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int X = 6;
  
    System.out.print(count_pairs(X) + "\n");
}
}
  
// This code is contributed by amal kumar choubey
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to count number of
# possible pairs of (a, b) such that
# their Bitwise OR gives the value X
  
# Function to count the pairs
def count_pairs(x):
  
    # Initializing answer with 1
    ans = 1;
  
    # Iterating through bits of x
    while (x > 0):
  
        # Check if bit is 1
        if (x % 2 == 1):
  
            # Multiplying ans by 3
            # if bit is 1
            ans = ans * 3;
  
        x = x // 2;
      
    return ans;
  
# Driver code
if __name__ == '__main__':
      
    X = 6;
  
    print(count_pairs(X));
  
# This code is contributed by amal kumar choubey 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
using System;
class GFG{
  
  // Function to count the pairs
  static int count_pairs(int x) 
  {
  
    // Initializing answer with 1
    int ans = 1;
  
    // Iterating through bits of x
    while (x > 0) 
    {
  
      // Check if bit is 1
      if (x % 2 == 1)
  
        // Multiplying ans by 3
        // if bit is 1
        ans = ans * 3;
  
      x = x / 2;
    }
    return ans;
  }
  
  // Driver code
  public static void Main(String[] args) 
  {
    int X = 6;
  
    Console.Write(count_pairs(X) + "\n");
  }
}
  
// This code is contributed by sapnasingh4991
chevron_right

Output: 
9

Time complexity: O(log(X))
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :