Print bitwise AND set of a number N

03 May, 2021
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++
Java
Python3
C#
PHP
Javascript
// CPP program to print all bitwise// subsets of N (Naive approach)#include <bits/stdc++.h>using namespace std; // function to find bitwise subsets// Naive approachvoid printSubsets(int n) {  for (int i = 0; i <= n; i++)    if ((n & i) == i)      cout << i << " ";} // Driver Codeint main() {     int n = 9;  printSubsets(n);  return 0;}
// 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.
# 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 coden = 9printSubsets(n) # This code is contributed by Anant Agarwal.
// 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.
<?php// PHP program to print all bitwise// subsets of N (Naive approach) // function to find bitwise subsets// Naive approachfunction 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?>
<script> // JavaScript program to print all bitwise// subsets of N (Efficient approach) // function to find bitwise    // subsets Efficient approach    function printSubsets(n)    {    for (let i = n; i > 0; i = (i - 1) & n)           document.write(i + " ");        document.write(" 0 ");           }// Driver code     let n = 9;    printSubsets(n); // This code is contributed by souravghosh0416.</script>
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:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to print all bitwise// subsets of N (Efficient approach) #include <bits/stdc++.h>using namespace std; // function to find bitwise subsets// Efficient approachvoid printSubsets(int n) {     for (int i = n; i > 0; i = (i - 1) & n)    cout << i << " ";  cout << 0;} // Driver Codeint main() {   int n = 9;   printSubsets(n);   return 0;}
// 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 Codepublic static void main(String[] args){    int n = 9;    printSubsets(n);}} // This code is contributed by ajit.
# Python 3 program to# print all bitwise# subsets of N# (Efficient approach) # function to find# bitwise subsets# Efficient approachdef printSubsets(n):    i=n    while(i != 0):        print(i,end=" ")        i=(i - 1) & n    print("0") # Driver Coden = 9printSubsets(n) # This code is contributed by# Smith Dinesh Semwal
// C# program to print all bitwise// subsets of N (Efficient approach)using System; public class GFG {     // function 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.
<?php// PHP program to print all bitwise// subsets of N (Efficient approach) // function to find bitwise subsets// Efficient approachfunction 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?>
<script> // Javascript program to print all bitwise// subsets of N (Efficient approach) // Function to find bitwise subsets// Efficient approachfunction printSubsets(n){    for(let i = n; i > 0; i = (i - 1) & n)        document.write(i +" ");        document.write("0" + "</br>");} // Driver codelet n = 9;      printSubsets(n); // This code is contributed by divyesh072019 </script>
Output : 
9 8 1 0
Time Complexity: O(K), where K is the number of bitwise subsets of N. 
Mithun Kumar
jit_t
Akanksha_Rai
souravghosh0416
divyesh072019
Bit Algorithms
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Builtin functions of GCC compiler
Calculate XOR from 1 to n.
Google Online Challenge 2020
Find two numbers from their sum and XOR
Highest power of 2 less than or equal to given number
Calculate square of a number without using *, / and pow()
Reverse actual bits of the given number
Count number of bits to be flipped to convert A to B
Find XOR of two number without using XOR operator