Print bitwise AND set of a number N

Given a number N, print all the numbers which are a bitwise AND set of the binary representation of N. Bitwise AND set of a number N is all possible numbers x smaller than or equal N such that N & i is equal to x for some number i.

Examples :

Input : N = 5
Output : 0, 1, 4, 5 
Explanation: 0 & 5 = 0 
             1 & 5 = 1
             2 & 5 = 0
             3 & 5 = 1
             4 & 5 = 4
             5 & 5 = 5  
So we get 0, 1, 4 and 5 in the 
bitwise subsets of N.

Input : N = 9
Output : 0, 1, 8, 9 

Simple Approach: A naive approach is to iterate from all numbers from 0 to N and check if (N&i == i). Print the numbers which satisfy the specified condition.



Below is the implementation of above idea:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print all bitwise
// subsets of N (Naive approach)
#include <bits/stdc++.h>
using namespace std;
  
// function to find bitwise subsets
// Naive approach
void printSubsets(int n) {
  for (int i = 0; i <= n; i++)
    if ((n & i) == i)
      cout << i << " ";
}
  
// Driver Code
int main() {
    
  int n = 9;
  printSubsets(n);
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA program to print all bitwise
// subsets of N (Naive approach)
class GFG {
      
    // function to find bitwise subsets
    // Naive approach
    static void printSubsets(int n)
    {
          
        for (int i = 0; i <= n; i++)
            if ((n & i) == i)
                System.out.print(i + " ");
    }
      
    // Driver function
    public static void main(String[] args)
    {
        int n = 9;
          
        printSubsets(n);
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print all bitwise 
# subsets of N (Naive approach)
def printSubsets(n):
      
    for i in range(n + 1):
          
        if ((n & i) == i):
            print(i ," ", end = "")
  
# Driver code
n = 9
printSubsets(n)
  
# This code is contributed by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all bitwise
// subsets of N (Naive approach)
using System;
  
class GFG {
      
    // function to find bitwise subsets
    // Naive approach
    static void printSubsets(int n)
    {
          
        for (int i = 0; i <= n; i++)
            if ((n & i) == i)
                Console.Write(i + " ");
    }
      
    // Driver function
    public static void Main()
    {
        int n = 9;
          
        printSubsets(n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print all bitwise
// subsets of N (Naive approach)
  
// function to find bitwise subsets
// Naive approach
function printSubsets($n
{
for ($i = 0; $i <= $n; $i++)
    if (($n & $i) == $i)
    echo $i." ";
}
  
// Driver Code
$n = 9;
printSubsets($n);
  
// This code is contributed by mits 
?>

chevron_right



Output :

0 1 8 9

Time Complexity : O(N)

Efficient Solution: An efficient solution is to use bitwise operators to find the subsets. Instead of iterating for every i, we can simply iterate for the bitwise subsets only. Iterating backward for i=(i-1)&n gives us every bitwise subset, where i starts from n and ends at 1.

Below is the implementation of above idea:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print all bitwise
// subsets of N (Efficient approach)
  
#include <bits/stdc++.h>
using namespace std;
  
// fucntion to find bitwise subsets
// Efficient approach
void printSubsets(int n) {
    
  for (int i = n; i > 0; i = (i - 1) & n)
    cout << i << " ";
  cout << 0;
}
  
// Driver Code
int main() {  
  int n = 9;  
  printSubsets(n);  
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all bitwise
// subsets of N (Efficient approach)
  
class GFG 
{
  
    // function to find bitwise 
    // subsets Efficient approach
    static void printSubsets(int n)
    {
    for (int i = n; i > 0; i = (i - 1) & n)
  
        System.out.print(i + " ");
        System.out.print(" 0 ");
      
    }
  
// Driver Code
public static void main(String[] args)
{
    int n = 9;
    printSubsets(n);
}
}
  
// This code is contributed by ajit.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to
# print all bitwise
# subsets of N
# (Efficient approach)
  
# fucntion to find
# bitwise subsets
# Efficient approach
def printSubsets(n):
    i=n
    while(i != 0):
        print(i,end=" ")
        i=(i - 1) & n
    print("0")
  
# Driver Code
n = 9 
printSubsets(n) 
  
# This code is contributed by
# Smith Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all bitwise
// subsets of N (Efficient approach)
using System;
  
public class GFG {
  
    // fucntion to find bitwise subsets
    // Efficient approach
    static void printSubsets(int n) {
      
    for (int i = n; i > 0; i = (i - 1) & n)
        Console.Write(i +" ");
        Console.WriteLine("0");
    }
      
    // Driver Code
    static public void Main () {
          
        int n = 9;
          
        printSubsets(n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print all bitwise
// subsets of N (Efficient approach)
  
// function to find bitwise subsets
// Efficient approach
function printSubsets($n)
{
  
    for ($i = $n; $i > 0; 
         $i = ($i - 1) & $n)
           
        echo $i." ";
    echo "0";
}
  
// Driver Code
$n = 9; 
printSubsets($n); 
  
// This code is contributed by mits 
?>

chevron_right



Output :

9 8 1 0

Time Complexity: O(K), where K is the number of bitwise subsets of N.



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.



Improved By : Mithun Kumar, jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.